我有一个sql存储过程将启动批量插入,我不需要挂起我的web服务,等待响应。我的问题是:如何在此上下文中实现BeginExecuteNonQuery?
答案 0 :(得分:2)
我认为这可以让你开始:
private void ExecuteCommandAsync(string sql)
{
SqlConnection conn = new SqlConnection(connectString + "Async=true;");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
conn.Open();
cmd.BeginExecuteNonQuery(new AsyncCallback(AsyncCommandCompletionCallback), cmd);
}
private void AsyncCommandCompletionCallback(IAsyncResult result)
{
SqlCommand cmd = null;
try
{
cmd = (SqlCommand)result.AsyncState;
cmd.EndExecuteNonQuery(result);
// Do some more work here...
}
catch (Exception ex)
{
// Handle errors here
}
finally
{
cmd.Connection.Close();
cmd.Dispose();
}
}