Namespace:
System.Data.SqlClient
Assembly:
System.Data (in System.Data.dll)
Visual Basic (Declaration)
Public Function ExecuteXmlReader As XmlReader
Dim instance As SqlCommand
Dim returnValue As XmlReader
returnValue = instance.ExecuteXmlReader()
public XmlReader ExecuteXmlReader()
public:
XmlReader^ ExecuteXmlReader()
public function ExecuteXmlReader() : XmlReader
| Exception | Condition |
|---|
| SqlException | An exception occurred while executing the command against a locked row. This exception is not generated when you are using Microsoft .NET Framework version 1.0. |
The CommandText property ordinarily specifies a Transact-SQL statement with a valid FOR XML clause. However, CommandText can also specify a statement that returns ntext or nvarchar data that contains valid XML, or the contents of a column defined with the xml data type.
A typical ExecuteXmlReader query can be formatted as in the following Microsoft Visual C# example:
SqlCommand command = new SqlCommand("SELECT * FROM dbo.Customers FOR XML AUTO, XMLDATA", SqlConn);
Note: |
|---|
This type of query only works with Microsoft SQL Server 2000 or later. For more information see Guidelines for Using the FOR XML Clause in SQL Server Books Online. |
When used with SQL Server 2005, this method can also be used to retrieve a single-row, single-column result set that contains XML data. In this case, if more than one row is returned, the ExecuteXmlReader method attaches the XmlReader to the value on the first row, and discards the rest of the result set.
When you use versions of SQL Server earlier than SQL Server 2005, while the XmlReader is being used the associated SqlConnection is busy serving the XmlReader. While in this state, no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the XmlReader is called. Starting with SQL Server 2005, the multiple active result set (MARS) feature allows for multiple actions using the same connection.
If you use ExecuteReader or BeginExecuteReader to access XML data, SQL Server will return any XML results greater than 2,033 characters in length in multiple rows of 2,033 characters each. To avoid this behavior, use ExecuteXmlReader or BeginExecuteXmlReader to read FOR XML queries. For more information, see article Q310378, "PRB: XML Data Is Truncated When You Use SqlDataReader," in the Microsoft Knowledge Base at http://support.microsoft.com.
The following example creates a SqlCommand and then executes it using ExecuteXmlReader. The example is passed a string that is a Transact-SQL FOR XML SELECT statement, and a string to use to connect to the data source.
Public Sub CreateXMLReader(ByVal queryString As String, _
ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As New SqlCommand(queryString, connection)
Dim reader As System.Xml.XmlReader = command.ExecuteXmlReader
End Using
End Sub
private static void CreateXMLReader(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
System.Xml.XmlReader reader = command.ExecuteXmlReader();
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
Reference
Other Resources