Async SqlComman 的问题

发布于 2024-08-27 10:28:01 字数 544 浏览 5 评论 0原文

我有超时问题,当我通过应用程序运行命令时,会抛出超时异常,但是当我直接在sql中运行它时,没有超时异常!

我的SP直接运行大约需要11分钟。 为了解决这个问题,我在这里找到了下面的代码,但是它不能正常工作! beginExecute 之后,IAsyncResult.iscomplete 立即变为 true!!!!

问题出在哪里?

IAsyncResult result = command.BeginExecuteNonQuery();

    int count = 0;
    while (!result.IsCompleted)
    {
        Console.WriteLine("Waiting ({0})", count++);
        System.Threading.Thread.Sleep(1000);
    }
    Console.WriteLine("Command complete. Affected {0} rows.",
    command.EndExecuteNonQuery(result));

问候

I have problem with Timeout, when I run a command through app, a timeout exception is thrown, but when I run it directly in sql there is no timeout exception!

my SP take about 11 min when I run it directly.
for solving this issue, I found below code here, but It doesn't work properly!
Immediately after beginExecute, IAsyncResult.iscomplete become true !!!!

where is the problem ?

IAsyncResult result = command.BeginExecuteNonQuery();

    int count = 0;
    while (!result.IsCompleted)
    {
        Console.WriteLine("Waiting ({0})", count++);
        System.Threading.Thread.Sleep(1000);
    }
    Console.WriteLine("Command complete. Affected {0} rows.",
    command.EndExecuteNonQuery(result));

regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏舞 2024-09-03 10:28:01

Increase the command timeout instead (SqlCommand.CommandTimeout) which by default is 30 seconds.

我纯我任性 2024-09-03 10:28:01

连接字符串默认为 15 秒超时。请参阅 MSDN< /a>.

您可以将连接字符串的超时时间更改为更长(connection timeout=600,超时时间为 10 分钟)。

有关连接字符串的更多信息,请参阅网站。

话虽如此,您应该考虑优化数据库和/或存储过程。 11 分钟对于一个存储过程来说是非常非常长的。您的表上有正确的索引吗?您的存储过程是以最优化的方式编写的吗?

更新:

您是否确定使用了正确的命令并且结果正确? IsComplete 为 true 几乎立即表明该命令确实已完成。

A connection string will default to a 15 second timeout. See on MSDN.

You can change the timeout on the connection string to last longer (connection timeout=600, for a 10 minute timeout).

See this site for more about connection strings.

Having said that, you should look at optimizing your database and/or stored procedure. 11 minutes for a stored procedure is very very long. Do you have the correct indexes on your tables? Is you stored procedure written in the most optimal way?

Update:

Have you made sure you are using the correct command and that the results are correct? IsComplete being true almost immediately suggests that the command has indeed finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文