BeginExecuteNonQuery without EndExecuteNonQuery(BeginExecuteNonQuery 没有 EndExecuteNonQuery)
问题描述
我有以下代码:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:BeginExecuteNonQuery 没有 EndExecuteNonQuery
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01