-1
我有一個sql存儲proc將啓動批量插入,我不需要我的web服務被掛斷,等待響應。我的問題是:我將如何在這種情況下實現BeginExecuteNonQuery?用BeginExecuteNonQuery編寫異步web服務?
我有一個sql存儲proc將啓動批量插入,我不需要我的web服務被掛斷,等待響應。我的問題是:我將如何在這種情況下實現BeginExecuteNonQuery?用BeginExecuteNonQuery編寫異步web服務?
我認爲這可能讓你開始:
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();
}
}