【问题标题】:Writing an async webservice with BeginExecuteNonQuery?使用 BeginExecuteNonQuery 编写异步 Web 服务?
【发布时间】:2014-04-17 14:10:11
【问题描述】:

我有一个 sql 存储过程,它将启动批量插入,我不需要挂起我的 web 服务,等待响应。我的问题是:在这种情况下我将如何实现 BeginExecuteNonQuery?

【问题讨论】:

    标签: c# .net web-services asynchronous


    【解决方案1】:

    我认为这可以帮助您入门:

    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();
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      相关资源
      最近更新 更多