Async SqlComman 的问题
我有超时问题,当我通过应用程序运行命令时,会抛出超时异常,但是当我直接在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
改为增加命令超时 (SqlCommand.CommandTimeout< /a>) 默认为 30 秒。
Increase the command timeout instead (SqlCommand.CommandTimeout) which by default is 30 seconds.
连接字符串默认为 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.