Home arrow ADO.NET arrow Page 6 - A Practical Comparison of ADO and ADO.NET
ADO.NET

A Practical Comparison of ADO and ADO.NET


Since "classic" ASP's humble beginnings, ADO data access classes and methods have been re-written to form ADO.NET. In this article Joe looks at the differences (in terms of classes, methods, and calling conventions) that exist between ADO and ADO.NET. He provides several practical examples that show us how to evolve from using ADO with ASP to using ADO.NET with C# and ASP.NET.

Author Info:
By: Joe O'Donnell
Rating: 4 stars4 stars4 stars4 stars4 stars / 72
March 01, 2002
TABLE OF CONTENTS:
  1. · A Practical Comparison of ADO and ADO.NET
  2. · Old versus new: ADO versus ASP.NET
  3. · Connecting to a database
  4. · Working with recordsets/rowsets
  5. · Calling stored procedures
  6. · Retrieving records as XML
  7. · Conclusion

print this article
SEARCH DEVARTICLES

A Practical Comparison of ADO and ADO.NET - Retrieving records as XML
(Page 6 of 7 )

SQL Server supports retrieving data using the FOR XML AUTO, FOR XML RAW, or FOR XML EXPLICIT keywords. Using ADO 2.6, we typically setup our query/stored procedure by adding FOR XML AUTO at the end of it and created a new ADO stream object, like this:

objComm.ActiveConnection = objConn

objComm.CommandType = 1 'adCmdText

objComm.CommandText = "SELECT TOP 1 * FROM authors FOR XML AUTO"

objComm.Properties("Output Stream") = objStream

objStream.Open

objComm.Execute , , 1024 'adExecuteStream

Response.Write objStream.ReadText


The output from the ASP/ADO code above return one record in the form of XML, which looks like this:

<authors au_id="172-32-1176" au_lname="Whitles" au_fname="Johnson" phone="408 496-7223" address="10932 Bigge Rd." city="Menlo Park" state="CA" zip="94025" contract="1"/>

ADO.NET makes it easier to work with XML, and provides a class call XmlReader, which exists in the System.Xml namespace. The ExecuteXmlReader() function returns an instance of the XmlReader class. We can loop through the results of that XmlReader class using its ReadOuterXml() function, like this:

<%@ import namespace="System.Data" %>

<%@ import namespace="System.Xml" %>

<%@ import namespace="System.Data.SqlClient" %>

<script language="c#" runat="server">

public void Page_Load(object sender, EventArgs e)

{

SqlConnection objConn = new SqlConnection("Server=(local); Database=Pubs; UId=sa; Pwd=");

objConn.Open();

SqlCommand objComm = new SqlCommand("SELECT TOP 1 * FROM authors FOR XML AUTO", objConn);

objComm.CommandType = CommandType.Text;

XmlReader objXR = objComm.ExecuteXmlReader();

while(objXR.Read())

{

Response.Write(objXR.ReadOuterXml());

}

}

</script>


The above script does exactly the same thing as the ADO/ASP example shown earlier, displaying an XML element in the browser.
blog comments powered by Disqus
ADO.NET ARTICLES

- Datasets in Microsoft.Net
- Latest Developments in the .Net World
- Introduction to .NET
- Automatic Generation of Single Table SQL Sta...
- Data Access in .NET using C#: Part 1
- All You Need To Know About ADO.NET: Part 2/2
- All You Need To Know About ADO.NET: Part 1/2
- Easing Transition From ASP and ADO to ASP.NE...
- A Practical Comparison of ADO and ADO.NET

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials