System.Data.SqlClient.SqlCommand.ExecuteXmlReader Method

Sends the SqlCommand.CommandText to the SqlCommand.Connection and builds an System.Xml.XmlReader object.

Syntax

public System.Xml.XmlReader ExecuteXmlReader ()

Returns

An System.Xml.XmlReader object.

Remarks

The SqlCommand.CommandText property ordinarily specifies a tsql statement with a valid FOR XML clause. However, SqlCommand.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 SqlCommand.ExecuteXmlReader query can be formatted as in the following Microsoft Visual C# example:

Example

SqlCommand command = new SqlCommand("SELECT * FROM dbo.Customers FOR XML AUTO, XMLDATA", SqlConn);

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 SqlCommand.ExecuteXmlReader method attaches the System.Xml.XmlReader to the value on the first row, and discards the rest of the result set.

The multiple active result set (MARS) feature allows for multiple actions using the same connection.

If you use SqlCommand.ExecuteReader or SqlCommand.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 SqlCommand.ExecuteXmlReader or erload:System.Data.SqlClient.SqlCommand.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.

Requirements

Namespace: System.Data.SqlClient
Assembly: System.Data (in System.Data.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0