gpt4 book ai didi

c# - 没有 EndExecuteNonQuery 的 BeginExecuteNonQuery

转载 作者:IT王子 更新时间:2023-10-29 04:46:39 25 4
gpt4 key购买 nike

我有以下代码:

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。

两个问题,首先这个block是因为using语句还是其他什么原因?第二,它会破坏什么吗?比如泄漏或连接问题?我只是想告诉 sql server 运行一个存储过程,但我不想等待它,我什至不关心它是否有效。那可能吗?感谢阅读。

最佳答案

这将不起作用,因为您在查询仍在运行时关闭了连接。最好的方法是使用线程池,如下所示:

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_ 否则你会泄漏内存。此规则的一个大异常(exception)是 Control.BeginInvoke。

关于c# - 没有 EndExecuteNonQuery 的 BeginExecuteNonQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1544905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com