System.Data.SqlClient.SqlCommand.BeginExecuteXmlReader Method

Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this System.Data.SqlClient.SqlCommand and returns results as an System.Xml.XmlReader object.

Syntax

public IAsyncResult BeginExecuteXmlReader ()

Returns

An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndExecuteXmlReader, which returns a single XML value.

Remarks

The SqlCommand.BeginExecuteXmlReader method starts the process of asynchronously executing a Transact-SQL statement that returns rows as XML, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the EndExecuteXmlReader method to finish the operation and retrieve the XML returned by the command. The SqlCommand.BeginExecuteXmlReader method returns immediately, but until the code executes the corresponding EndExecuteXmlReader method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same System.Data.SqlClient.SqlCommand object. Calling the EndExecuteXmlReader before the command's execution is completed causes the System.Data.SqlClient.SqlCommand object to block until the execution is finished.

The SqlCommand.CommandText property ordinarily specifies a Transact-SQL statement with a valid FOR XML clause. However, CommandText can also specify a statement that returns ntext data that contains valid XML.

A typical SqlCommand.BeginExecuteXmlReader(AsyncCallback, object) query can be formatted as in the following C# example:

Example

SqlCommand command = new SqlCommand("SELECT ContactID, FirstName, LastName FROM dbo.Contact FOR XML AUTO, XMLDATA", SqlConn);

This method can also be used to retrieve a single-row, single-column result set. In this case, if more than one row is returned, the EndExecuteXmlReader 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 lets multiple actions use the same connection.

Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters are sent, this method may block during writes. After the command is sent, the method returns immediately without waiting for an answer from the server--that is, reads are asynchronous. Although command execution is asynchronous, value fetching is still synchronous.

Because this overload does not support a callback procedure, developers need to either poll to determine whether the command has completed, using the IAsyncResult.IsCompleted property of the IAsyncResult returned by the SqlCommand.BeginExecuteXmlReader method; or wait for the completion of one or more commands using the IAsyncResult.AsyncWaitHandle property of the returned IAsyncResult.

If you use SqlCommand.ExecuteReader or SqlCommand.BeginExecuteReader to access XML data, SQL Server returns 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: 2.0.0.0