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, given a callback procedure and state information.
- 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.
An IAsyncResult that can be used to poll, wait for results, or both; this value is also needed when invoking SqlCommand.EndExecuteReader(IAsyncResult), which returns a System.Data.SqlClient.SqlDataReader instance which can be used to retrieve the returned rows.
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 cause the System.Data.SqlClient.SqlCommand object to block until the execution is finished.
The callback parameter lets you specify an AsyncCallback delegate that is called when the statement has completed. You can call the SqlCommand.EndExecuteReader(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 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 the callback procedure executes from within a background thread supplied by the Microsoft .NET runtime, it is very important that you take a rigorous approach to handling cross-thread interactions from within your applications. For example, you must not interact with a form's contents from within your callback procedure; should you have to update the form, you must switch back to the form's thread in order to do your work. The example in this topic demonstrates this behavior.
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 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.