Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用BeginExecutenQuery数据库的asp.net异步调用未触发_Asp.net - Fatal编程技术网

使用BeginExecutenQuery数据库的asp.net异步调用未触发

使用BeginExecutenQuery数据库的asp.net异步调用未触发,asp.net,Asp.net,我的代码触发ExecuteOnQueryAsyc。在调试中,我看到它命中了command.beginexecutenquery(新的AsyncCallback(AsyncCommandCompletionCallback),command); 但是被调用的sp并没有真正被触发。我做错了什么 public static void ExecuteNonQueryAsyc(string procedureName, IList<SqlParameter> parameters) {

我的代码触发ExecuteOnQueryAsyc。在调试中,我看到它命中了command.beginexecutenquery(新的AsyncCallback(AsyncCommandCompletionCallback),command); 但是被调用的sp并没有真正被触发。我做错了什么

public static void ExecuteNonQueryAsyc(string procedureName, IList<SqlParameter> parameters)
{
    using (SqlConnection connection = new SqlConnection(Config.GetDbConnection() + ";Async=true;"))
    {
        SqlCommand 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();
    }
}
public static void ExecuteNonQueryAsyc(字符串过程重命名,IList参数)
{
使用(SqlConnection=newsqlconnection(Config.GetDbConnection()+“Async=true;”)
{
SqlCommand=connection.CreateCommand();
command.CommandText=procedureName;
command.CommandType=CommandType.storedProcess;
if(参数!=null)
for(int index=0;index
也有同样的问题,所以即使答案在评论中:

using (SqlConnection connection = new SqlConnection(Config.GetDbConnection() + ";Async=true;"))

导致连接在命令完成之前关闭(可能是在它真正被调用之前,因为它位于另一个线程上,可能需要几秒钟才能启动和启动)。

是否需要“新的AsyncCallback(AsycommandCompletionCallback)”?回调函数名“AsycommandCompletionCallback”不应该只起作用吗?不是问题的原因,但是为什么不在回调中也使用“using”呢?不确定。我从这里得到了解决方案:另外,您是否尝试过在cmd.endexecutenquery行上放置一个断点,以查看是否达到了该断点?因此,我没有很好地遵循他的示例。他说您在使用范围内拥有SqlConnection。当使用范围结束时,连接关闭。因此,在您的情况下,连接在完成(可能)之前关闭。我将更改ExecuteOnQueryAsyc以摆脱使用块,并添加一个try-catch块,如果引发异常,则关闭连接并重新引发异常,否则让AsyncCommandCompletionCallback关闭连接。public static void ExecuteNonQueryAsyc(字符串过程重命名,IList参数)如果您认为它关闭连接太早,可以尝试加入一个Thread.Sleep(10000)来给它足够的时间来完成。然后检查SP是否按预期运行。