Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Database operations can take significant time to complete. To keep a user interface responsive or avoid blocking a request thread, use asynchronous database methods instead of waiting synchronously for a command to finish.
Install the Microsoft.Data.SqlClient NuGet package for SQL client code. For new applications, use OpenAsync and the ExecuteNonQueryAsync, ExecuteReaderAsync, and ExecuteXmlReaderAsync methods with await. These methods perform asynchronous input/output (I/O) without requiring you to create a background thread.
Microsoft.Data.SqlClient also supports the callback, wait-handle, and polling patterns described in this section. The BeginExecuteNonQuery, BeginExecuteReader, and BeginExecuteXmlReader methods pair with EndExecuteNonQuery, EndExecuteReader, and EndExecuteXmlReader. Don't add Asynchronous Processing=true to the connection string; Microsoft.Data.SqlClient doesn't support that keyword.
Note
For new code, use the Task-based asynchronous pattern. The examples in this section show the supported Begin/End pattern for applications that need callbacks, wait handles, or polling.
Keep each connection and command alive until its operation completes. Observe exceptions when you call the matching End method or await the task. In a desktop application, update controls only from the user interface thread.
In This Section
Windows Applications Using Callbacks Provides an example demonstrating how to execute an asynchronous command safely, correctly handling interaction with a form and its contents from a separate thread.
ASP.NET Applications Using Wait Handles Provides an example demonstrating how to execute multiple concurrent commands from an ASP.NET page, using Wait handles to manage the operation at completion of all the commands.
Polling in Console Applications Provides an example demonstrating the use of polling to wait for the completion of an asynchronous command execution from a console application. This technique is also valid in a class library or other application without a user interface.