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.
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
objComm.Execute , , 1024 'adExecuteStream
The output from the ASP/ADO code above return one record in the form of XML, which looks like this:
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=");
SqlCommand objComm = new SqlCommand("SELECT TOP 1 * FROM authors FOR XML AUTO", objConn);
objComm.CommandType = CommandType.Text;
XmlReader objXR = objComm.ExecuteXmlReader();
The above script does exactly the same thing as the ADO/ASP example shown earlier, displaying an XML element in the browser.