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