System.Data.SqlClient.SqlCommand.BeginExecuteReader Method

Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this System.Data.SqlClient.SqlCommand, and retrieves one or more result sets from the server.

Syntax

public IAsyncResult BeginExecuteReader ()

Returns

An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking SqlCommand.EndExecuteReader(IAsyncResult), which returns a System.Data.SqlClient.SqlDataReader instance that can be used to retrieve the returned rows.

Remarks

The SqlCommand.BeginExecuteReader method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that returns rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the SqlCommand.EndExecuteReader(IAsyncResult) method to finish the operation and retrieve the System.Data.SqlClient.SqlDataReader returned by the command. The SqlCommand.BeginExecuteReader method returns immediately, but until the code executes the corresponding SqlCommand.EndExecuteReader(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.EndExecuteReader(IAsyncResult) before the command's execution is completed causes the System.Data.SqlClient.SqlCommand object to block until the execution is finished.

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. This means that calls to SqlDataReader.Read may block if more data is required and the underlying network's read operation blocks.

Because this overload does not support a callback procedure, developers must either poll to determine whether the command has completed, using the IAsyncResult.IsCompleted property of the IAsyncResult returned by the SqlCommand.BeginExecuteReader 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 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