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 .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:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="AsynchronousConnString" connectionString="Data Source=SANJAY-17-PC;Initial
Catalog=Testdb; Integrated Security=True; Async=True; User ID=sa;Password=sa@12345;
MultipleActiveResultSets=True
" providerName="System.Data.SqlClient"/>
  </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>


Now, use the following code:

Connection.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsynchronousConnection
{

   
class Connection
    {
       
static async Task<int> Method(SqlConnection conn, SqlCommand cmd)
        {
           
await conn.OpenAsync();
            
await cmd.ExecuteNonQueryAsync();
           
return 1;           
        }
       
public static void Main()
        {
           
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["AsynchronousConnString"].ConnectionString);
           
Try
            {
               
SqlCommand cmd = new SqlCommand("Select * from StudentTable WAITFOR DELAY '00:00:01'
"
, conn);
               
int result = Connection.Method(conn, cmd).Result;
               
SqlDataReader read = cmd.ExecuteReader();              
               
while (read.Read())
                {
                   
Console.WriteLine(String.Format("{0},{1},{2},{3}", read[0], read[1], read[2], read[3]));                  
                }
               
Console.ReadLine();
            }
           
catch (SqlException )
            {
              
            }          
           
Finally
            {
              conn.Close();               
            }
        }
    }
}


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.

Up Next
    Ebook Download
    View all
    Learn
    View all