using System.Data.SqlClient; // Connection string for a SQL Server instance string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"; // Create a new SqlConnection object SqlConnection connection = new SqlConnection(connectionString); // Call OpenAsync to asynchronously open the connection await connection.OpenAsync(); // Connection is now open and ready to use
using System.Data.SqlClient; // Connection string for a SQL Server instance string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"; // Create a new SqlConnection object SqlConnection connection = new SqlConnection(connectionString); // Call OpenAsync to asynchronously open the connection await connection.OpenAsync(); // Create a SqlCommand object for executing a query SqlCommand command = new SqlCommand("SELECT * FROM myTable", connection); // Call ExecuteReaderAsync to asynchronously execute the query SqlDataReader reader = await command.ExecuteReaderAsync(); // Read data from the SqlDataReader object while (await reader.ReadAsync()) { // Process the row of data } // Close the SqlDataReader and SqlConnection objects reader.Close(); connection.Close();In both of these examples, we are utilizing the System.Data.SqlClient package library to asynchronously open a connection to a SQL Server instance and execute a query. By using the asynchronous OpenAsync and ExecuteReaderAsync methods, our code can continue executing while we wait for the database operation to complete.