C# .NET - What is the use of BeginExecuteNonQuery() and EndExecuteNonQuery()?

Asked By Rajendra Reddy on 31-Dec-13 06:20 AM
I need to delete huge data, I wanted to try with BeginExecuteNonQuery()  and EndExecuteNonQuery().

Please correct me whether following code snippet works fine or should I need to change something?

str deleteString = "Delete from ABC"
try
{
if (connection.State != ConnectionState.Open) { connection.Open(); }

SqlCommand deleteCommand = new SqlCommand(deleteString, connection))                   
IAsyncResult result = deleteCommand.BeginExecuteNonQuery();
deleteCommand.EndExecuteNonQuery(result);
}
finally
{
if (connection.State != ConnectionState.Closed) { connection.Close(); }
}
Robbe Morris replied to Rajendra Reddy on 31-Dec-13 08:26 AM
All of the work (and time consuming effort) is done on the server.  I doubt if the async approach gets you much here.   This approach is most useful when you have lots of transactions that you need to process asynchronously.
Rajendra Reddy replied to Robbe Morris on 01-Jan-14 01:34 AM
Thanks for replay "Robbe Morries",

I have to delete record from multiple table that too doing in single connection string. So wanted to try with asynchronous approach.

Robbe Morris replied to Rajendra Reddy on 01-Jan-14 01:17 PM
Perhaps the threadpool is a good option then.

http://www.nullskull.com/faq/1488/threadpoolqueueuserworkitem-multithreading-code-sample.aspx

Keep in mind, .net is managing connection pooling for you.  So, doing it using a single connected SqlConnection is improving your performance.