SqlCommand::BeginExecuteReader Method ()
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this SqlCommand, and retrieves one or more result sets from the server.
Assembly: System.Data (in System.Data.dll)
public: [HostProtectionAttribute(SecurityAction::LinkDemand, ExternalThreading = true)] IAsyncResult^ BeginExecuteReader()
Any error that occurred while executing the command text.
A timeout occurred during a streaming operation. For more information about streaming, see SqlClient Streaming Support.
The EndExecuteReader method to finish the operation and retrieve the SqlDataReader returned by the command. The method returns immediately, but until the code executes the corresponding EndExecuteReader method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same SqlCommand object. Calling the EndExecuteReader before the command's execution is completed causes the SqlCommand object to block until the execution is finished.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
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 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 IsCompleted property of the IAsyncResult returned by the method; or wait for the completion of one or more commands using the AsyncWaitHandle property of the returned IAsyncResult.
If you use ExecuteReader or 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 console application starts the process of retrieving a data reader asynchronously. While waiting for the results, this simple application sits in a loop, investigating the IsCompleted property value. As soon as the process has completed, the code retrieves the SqlDataReader and displays its contents.
Available since 2.0