BeginExecuteNonQuery without EndExecuteNonQuery

后端 未结 5 1420
梦谈多话
梦谈多话 2020-12-03 10:40

I have the following code:

using (SqlConnection sqlConnection = new SqlConnection(\"blahblah;Asynchronous Processing=true;\")
{
    using (SqlCommand command         


        
5条回答
  •  抹茶落季
    2020-12-03 11:04

    1. You can't close the connection after you submit the BeginExceuteNotQuery. It will abort the execution. Remove the using block.

    2. In order to close the connection, you must know when the call has completed. For that you must call EndExecuteNonQuery, usually from a callback:

    .

    command.BeginExecuteNonQuery(delegate (IAsyncResult ar) {
       try { command.EndExecuteNonQuery(ar); }
       catch(Exception e) { /* log exception e */ }
       finally { sqlConnection.Dispose(); }
       }, null);
    

    If you want to submit a query and don't care about the results, see Asynchronous T-SQL execution for a reliable pattern that ensures execution even if client diconnects or crashes.

提交回复
热议问题