Microsoft KB Archive/316701

= HOW TO: Use the ExecuteXmlReader Method of the SqlCommand Class in Visual C# .NET =

Article ID: 316701

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q316701



For a Microsoft Visual Basic .NET version of this article, see 316016.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * Requirements
 * Create and Run the Sample Code



SUMMARY
You can use the ExecuteXmlReader method of the System.Data.SqlClient.SqlCommand class to load the results of SQL Extensible Markup Language (XML) queries from Microsoft SQL Server into a System.Xml.XmlReader object. You can use this method to run server-side for XML queries and to run queries that return well-formed XML data as text.

NOTE: This method is not available in the OLE DB .NET managed provider (it is specific to the SQL Server .NET managed provider) and does not work with a SQL 7.0 database.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows NT 4.0 Server, Windows 2000 Server, or another Microsoft operating system that can host SQL Server 2000
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with XML and SQL XML queries.

back to the top

Create and Run the Sample Code
 Create a new Windows application in Visual C# .NET, and then add a Button control to Form1.  Add the following code to the Click event of the Button: try

{

System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection;

cn.ConnectionString=&quot;data source=(local);initial catalog=pubs;User ID=myuser;Password=mypassword&quot;;

cn.Open;

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand;

cmd.Connection = cn;

cmd.CommandText = &quot;SELECT * FROM authors FOR XML AUTO, XMLDATA&quot;;

System.Xml.XmlReader xmlr = cmd.ExecuteXmlReader;

xmlr.Read;

while(xmlr.ReadState != System.Xml.ReadState.EndOfFile)

{

System.Diagnostics.Debug.WriteLine(xmlr.ReadOuterXml);

}

MessageBox.Show(&quot;Reached the end. Check the output window.&quot;);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}                    Modify the settings in the connection string as appropriate for your environment. Run the program in debug mode (from Start or press F5), and then click the button. The Xmlr XmlReader object contains the results of the SQL XML query. The Output window contains the contents of the XmlReader object.



back to the top

Keywords: kbhowtomaster kbsqlclient kbsystemdata KB316701

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.