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, using a callback procedure.

Syntax

public IAsyncResult BeginExecuteXmlReader (AsyncCallback callback, object stateObject)

Parameters

callback
An AsyncCallback delegate that is invoked when the command's execution has completed. Pass null (Nothing in Microsoft Visual Basic) to indicate that no callback is required.
stateObject
A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the IAsyncResult.AsyncState property.

Returns

An IAsyncResult that can be used to poll, wait for results, or both; this value is also needed when the SqlCommand.EndExecuteXmlReader(IAsyncResult) is called, which returns the results of the command as XML.

Remarks

The SqlCommand.BeginExecuteXmlReader method starts the process of asynchronously executing a Transact-SQL statement or stored procedure 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 SqlCommand.EndExecuteXmlReader(IAsyncResult) method to finish the operation and retrieve the requested XML data. The SqlCommand.BeginExecuteXmlReader method returns immediately, but until the code executes the corresponding SqlCommand.EndExecuteXmlReader(IAsyncResult) 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 SqlCommand.EndExecuteXmlReader(IAsyncResult) 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 data that contains valid XML. 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 SqlCommand.EndExecuteXmlReader(IAsyncResult) method attaches the System.Xml.XmlReader to the value on the first row, and discards the rest of the result set.

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 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 SqlCommand.EndExecuteXmlReader(IAsyncResult) 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.

The callback parameter lets you specify an AsyncCallback delegate that is called when the statement has completed. You can call the SqlCommand.EndExecuteXmlReader(IAsyncResult) method from within this delegate procedure, or from any other location within your application. In addition, you can pass any object in the stateObject parameter, and your callback procedure can retrieve this information using the IAsyncResult.AsyncState property.

Note that the command text and parameters are sent to the server synchronously. If a large command or many parameters is 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.

All errors that occur during the execution of the operation are thrown as exceptions in the callback procedure. You must handle the exception in the callback procedure, not in the main application. See the example in this topic for additional information on handling exceptions in the callback procedure.

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: 2.0.0.0