How to execute long running SQL query or stored-procedure from .net

How to make Asynchronous call to MS SQL server from  .net

Execute T-SQL asynchronously from .net

Click here to download sample project...

Below are the 2 main issues arises when your application is intended to deal with huge data-

 

  • SQL Server takes significant time to process (long running SQL statements) which leads to block the execution of your .net code
  • Main thread or UI thread also get blocked till the response from the Sql server.

 

These issues are the serious issues while building interactive applications. User patience is an unpredictable parameter and user’s reaction against long waiting screen is uncertain. At-least UI shouldn't be freeze to engage the user and make him to wait for the result.

Since, transactional SQL statements will definitely take time to process the things, the quickest solution sought is on the application programming level. Also, it is known that MS SQL server takes each & every call as synchronous, even if you change the connection string property AsynchronousProcessing to true.  It is client application(C#, .net) which gets affected. So, below are the some widely used solutions-

 

  1. Cancellation Token mechanism - so that user can cancel ongoing longer execution if they unwilling to wait
  2. Callback mechanism - So that UI thread can't get blocked.

 

Cancellation mechanism:

It has many limitations, for example, a fired Sql command can be cancelled by firing SqlCommand.Cancel() but for this that command object must be persist. SqlReader's Close or Read is designed to ignore this Cancel() call if this call is not placed before them and so on. 

Callback mechanism:

It is a great programming style that solves many issues and help you to build more interactive UI based applications. Instead of holding execution on a long running statement(s), it allows you to carry on to next line of code. In this post, we will implement this functionality to get execute Sql query asynchronously.

In our sample project which is a WPF application, we will use Callback mechanism. Let's add following controls on the Window-

 

  1. Button             ( btnExecuteQuery )
  2.  ListBox           ( listboxResult )
  3. TextBox          ( textboxQuery )

 

Idea is to fire asynchronous call to MS SQL for the T-SQL statement written in textboxQuery on btnExecuteQuery click event.

For executing MS SQL statements in asynchronous, we have to-

 

  1. Set the Asynchronous Processing property of ConnectionString to true
  2. Use command.BeginExecuteNonQuery() and command.EndExecuteNonQuery()

 

At connection level, a specification is required in connection string for asynchronous processing.

Ex- AsynchronousProcessing = true

clip_image002

At SqlCommand execution level, instead of ExecuteReader() we need to use BeginExecuteReader() and EndExecuteReader() methods to achieve asynchronous execution. BeginExecuteReader takes 2 arguments -

1.       A callback procedure/method

2.       User-state (called 'StateObject' which holds the status of an asynchronous operation)

Ex.

clip_image004

Generally, state-object is the object which will required in the callback method to process the things. Here, we are passing SqlCommand object because our Sql query will be executed in different function inside different worker thread. Our SqlCommand object is already initialized and ready to use by callback function.

Let’s focus on our callback function, which has been passed to AsyncCallback() inside BeginExecuteReader(). It has syntax as below-

clip_image006

Please note IAsyncResult type, which is required to hold the status of an asynchronous operation.

To un-box the AsynState back to the SqlCommand, we need to typecast as below-

clip_image008

So far, we have understand the required logic & way. Let's wired the things together. Our button click event handler code would looks like-

private void btnExecuteQuery_Click(object sender, RoutedEventArgs e)
{
    SqlConnectionStringBuilder connectionBuilder
        = new SqlConnectionStringBuilder("Network Address=localhost; Initial Catalog=DemoDatabase; Integrated Security=true;")
    {
        ConnectTimeout = 4000,
        AsynchronousProcessing = true
    };
 
    SqlConnection conn = new SqlConnection(connectionBuilder.ConnectionString);
    SqlCommand cmd = new SqlCommand(textboxQuery.Text, conn);
    try
    {
        conn.Open();
 
        //The actual T-SQL execution happens in a separate work thread.
        cmd.BeginExecuteReader(new AsyncCallback(MyCallbackFunction), cmd);
    }
    catch (SqlException se)
    {
        //ToDo : Swallow exception log
    }
    catch (Exception ex)
    {
        //ToDo : Swallow exception log
    }
}

 

And the callback function looks like-

private void MyCallbackFunction(IAsyncResult result)
{
 try
  {
    //un-box the AsynState back to the SqlCommand
    SqlCommand cmd = (SqlCommand)result.AsyncState;
    SqlDataReader reader = cmd.EndExecuteReader(result);
    while (reader.Read())
     {
       Dispatcher.BeginInvoke( new delegateAddTextToListbox(AddTextToListbox),
       reader.GetString(0));
     }
 
    if (cmd.Connection.State.Equals(ConnectionState.Open))
     {
       cmd.Connection.Close();
     }
  }
 catch (Exception ex)
  {
   //ToDo : Swallow exception log
  }
}
 

Great! We have implemented it! But wait, we need few more tackle. J Since, our sample project is a Windows based WPF application, form’s controls & method are not accessible from other than main UI thread. Thus, accessing Windows Form’s controls listboxResult will produce exception (InvalidOperationException) - { "The calling thread cannot access this object because a different thread owns it." } 

clip_image010

So, we need a different approach to access Windows Form’s controls & associated methods and i.e. Delegate. We will shift listboxResult accessing code inside a function and we will call that function through delegate.

Let’s create a delegate which can point out any function that will accept one string argument and return void.

clip_image012

Next, is to create a method that will accept string and add that to Windows Form’s control listbox-

clip_image014

Now, we can use delegate in our callback function to add the result into listbox as-

clip_image016

Done! Now, worker thread will be able to add the result to the controls by method through delegate call. Dispatcher.BeginInvoke() is used to execute delegate asynchronously with the specified arguments. This method is under System.Windows.Threading namespace.

clip_image018

Summary :

C# 5.0 introduces a new asynchronous programming model i.e. async & await . It no longer require requires pair codes like BeginExecute & EndEquecute. A task is declared for asynchronous programming and rest is done automatically by runtime. Ex.- var task = await cmd.ExecuteReaderAsyc();

And method containing these task based statement(s) must must be decorated with async
ex.- private async void MyAsyncFunction() {…}

 

 

@AnilAwadh