asp.net Async call using BeginExecuteNonQuery db not firing
my code fires off ExecuteNonQueryAsyc. In debug I see it hit command.BeginExecuteNonQuery(new AsyncCallback(AsyncCommandCompletionCallback), command); line but the sp is not really being fired off that gets called. what am I doing wrong?
public static void ExecuteNonQueryAsyc(string procedureName, IList<SqlParameter> parameters)
{
using (SqlConnection connection = new SqlConnection(Config.GetDbConnection() + ";Async=true;"))
{
S开发者_如何学编程qlCommand command = connection.CreateCommand();
command.CommandText = procedureName;
command.CommandType = CommandType.StoredProcedure;
if (parameters != null)
for (int index = 0; index < parameters.Count; index++)
command.Parameters.Add(parameters[index]);
connection.Open();
command.BeginExecuteNonQuery(new AsyncCallback(AsyncCommandCompletionCallback), command);
}
}
static void AsyncCommandCompletionCallback(IAsyncResult result)
{
SqlCommand cmd = null;
try
{
// Get our command object from AsyncState, then call EndExecuteNonQuery.
cmd = (SqlCommand)result.AsyncState;
cmd.EndExecuteNonQuery(result);
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Connection.Close();
cmd.Dispose();
}
}
Just had this same problem, so even though the answer is in the comments:
using (SqlConnection connection = new SqlConnection(Config.GetDbConnection() + ";Async=true;"))
causes the connection to close before the command is completed (possibly before it even really gets called as it's on another thread which probably takes a few ticks to fire up and get started).
精彩评论