SqlCommand.BeginExecuteNonQuery Method (AsyncCallback, Object)
Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by this SqlCommand, given a callback procedure and state information.
Assembly: System.Data (in System.Data.dll)
[<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)>] member BeginExecuteNonQuery : callback:AsyncCallback * stateObject:Object -> IAsyncResult
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.
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 BeginExecuteNonQuery method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that does not return rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the EndExecuteNonQuery method to finish the operation. The BeginExecuteNonQuery method returns immediately (CommandTimeout has no effect on ), but until the code executes the corresponding EndExecuteNonQuery method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same SqlCommand object. Calling the EndExecuteNonQuery before the command's execution is completed causes the 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 EndExecuteNonQuery method from within this delegate procedure, or from any other location within your application. In addition, you can pass any object in the asyncStateObject parameter, and your callback procedure can retrieve this information using the 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.
Because the callback procedure executes from within a background thread supplied by the Microsoft .NET common language 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.
The following Windows application demonstrates the use of themethod, executing a Transact-SQL statement that includes a delay of several seconds (emulating a long-running command).
This example demonstrates many important techniques. This includes calling a method that interacts with the form from a separate thread. In addition, this example demonstrates how you must block users from executing a command multiple times concurrently, and how you must make sure that the form does not close before the callback procedure is called.
To set up this example, create a new Windows application. Put a Button control and a Label control on the form (accepting the default name for each control). Add the following code to the form's class, modifying the connection string as needed for your environment.
Available since 2.0