BeginExecuteNonQuery的无EndExecuteNonQuery [英] BeginExecuteNonQuery without EndExecuteNonQuery

查看:338
本文介绍了BeginExecuteNonQuery的无EndExecuteNonQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

using (SqlConnection sqlConnection = new SqlConnection("blahblah;Asynchronous Processing=true;")
{
    using (SqlCommand command = new SqlCommand("someProcedureName", sqlConnection))
    {
        sqlConnection.Open();

        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@param1", param1);

        command.BeginExecuteNonQuery();
    }
}

我从来没有叫EndExecuteNonQuery。

I never call EndExecuteNonQuery.

有两个问题,第一个将这个块,因为using语句或任何其他原因吗?其次,它会破坏什么?就像泄漏或连接问题?我只是想告诉SQL Server运行存储过程,但我不想等待它,我甚至不关心,如果它的工作原理。那可能吗?感谢您的阅读。

Two questions, first will this block because of the using statements or any other reason? Second, will it break anything? Like leaks or connection problems? I just want to tell sql server to run a stored procedure, but I don't want to wait for it and I don't even care if it works. Is that possible? Thanks for reading.

推荐答案

因为你关闭连接,同时查询仍在运行这是行不通的。要做到这一点,最好的方法是使用线程池,像这样的:

This won't work because you're closing the connection while the query is still running. The best way to do this would be to use the threadpool, like this:

ThreadPool.QueueUserWorkItem(delegate {
    using (SqlConnection sqlConnection = new SqlConnection("blahblah;Asynchronous Processing=true;") {
        using (SqlCommand command = new SqlCommand("someProcedureName", sqlConnection)) {
            sqlConnection.Open();

            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@param1", param1);

            command.ExecuteNonQuery();
        }
    }
});

在一般情况下,当你调用Begin_Whatever_,你通常必须调用End_Whatever_,否则你会泄漏内存。最大的例外是Control.BeginInvoke。

In general, when you call Begin_Whatever_, you usually must call End_Whatever_ or you'll leak memory. The big exception to this rule is Control.BeginInvoke.

这篇关于BeginExecuteNonQuery的无EndExecuteNonQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆