Asynchronous Connection in .NET Framework 4.5

This article explains how to create an Asynchronous Connection in .NET Framework 4.5.
 
Asynchronous programming in the .NET Framework Data Provider for SQL Server (SqlClient) includes enhancements to support asynchronous programming functionality that was introduced in .NET Framework 4.5. And the new asynchronous features of the .NET Framework are also being extended to ADO.NET. The SqlConnection object has the OpenAsync method to open a connection asynchronously.
 
So, let's proceed with the following procedure:
  • Create an Asynchronous Connection
  • Create a Command in Asynchronous programming
Create a new project using "File" -> "New" -> "Project..." then select "Console Application". Name it "Asynchronous Connection".
 
/Asynchronous Connection
 
In the App.config file create the database. Set on the connection string: Asynchronous operation to work, the attribute Async=True; as shown here:
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <connectionStrings>  
  4.     <add name="AsynchronousConnString" connectionString="Data Source=SANJAY-17-PC;Initial  
  5. Catalog=Testdb; Integrated Security=True; Async=True; User ID=sa;Password=sa@12345;  
  6. MultipleActiveResultSets=True " providerName="System.Data.SqlClient"/>  
  7.   </connectionStrings>  
  8.     <startup>  
  9.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
  10.     </startup>  
  11. </configuration> 
Now, use the following code:
 
Connection.cs
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Configuration;    
  4. using System.Data.SqlClient;    
  5. using System.Linq;    
  6. using System.Text;    
  7. using System.Threading.Tasks;    
  8.     
  9. namespace AsynchronousConnection    
  10. {    
  11.     
  12.     class Connection    
  13.     {    
  14.         static async Task<int> Method(SqlConnection conn, SqlCommand cmd)    
  15.         {    
  16.             await conn.OpenAsync();    
  17.             await cmd.ExecuteNonQueryAsync();    
  18.             return 1;               
  19.         }    
  20.         public static void Main()    
  21.         {    
  22.             SqlConnection conn = new    
  23. SqlConnection(ConfigurationManager.ConnectionStrings["AsynchronousConnString"].ConnectionString);    
  24.             try    
  25.             {    
  26.                 SqlCommand cmd = new SqlCommand("Select * from StudentTable WAITFOR DELAY '00:00:01' ", conn);  
  27.    
  28.                 int result = Connection.Method(conn, cmd).Result;    
  29.                 SqlDataReader read = cmd.ExecuteReader();                  
  30.                 while (read.Read())    
  31.                 {    
  32.                     Console.WriteLine(String.Format("{0},{1},{2},{3}", read[0], read[1], read[2], read[3]));                      
  33.                 }    
  34.                 Console.ReadLine();    
  35.             }    
  36.             catch (SqlException )    
  37.             {    
  38.                   
  39.             }              
  40.             finally    
  41.             {    
  42.               conn.Close();                   
  43.             }    
  44.         }    
  45.     }    
  46. }   
Now, See the following screen; select "SQL Server database" > "StudentTable".
 
Asynchronous Connection1
 
Run the application then select "Show data" > "Students Table" as in the following:
 
Asynchronous Connection2
 
Now retrieve the data using the SQL Server database and create an Asynchronous Connection operation with async and await modifiers in .NET Framework 4.5.
 
I hope this article is useful. If you have any other questions then please provide your comments below.