C# 什么原因导致 LINQ to SQL 中的超时过期 SqlExceptions?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/840334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:41:07  来源:igfitidea点击:

What causes Timeout expired SqlExceptions in LINQ to SQL?

c#sql-serversql-server-2005linq-to-sqlsqlexception

提问by Jason Baker

My application keeps running into Timeout Expired SqlExceptions. The thing is that this query is one that will simply have to run for a decent amount of time. I'm having trouble figuring out where this exception is occurring though. Is this a timeout that's created at the database server or is it happening in my program? Or if it could be both, how do I figure out which one it is?

我的应用程序不断遇到超时过期 SqlExceptions。问题是这个查询只需要运行相当长的时间。不过,我无法弄清楚发生此异常的位置。这是在数据库服务器上创建的超时还是发生在我的程序中?或者如果两者都可以,我如何确定它是哪一个?

And lastly, how do I extend the timeout period?

最后,如何延长超时时间?

采纳答案by Garry Shutler

It is likely that you are running over the CommandTimeoutset on your DataContext. This defaults to 30 seconds.

您可能正在运行DataContext 上设置的CommandTimeout。这默认为 30 秒。

All you need to do is set the property on the DataContext before you execute your query.

您需要做的就是在执行查询之前在 DataContext 上设置属性。

回答by JoshBerke

This is probally occuring becuase the default timeout is 30 seconds. You can change this by setting a Timeout attribute on the connection string. Alternatley you can set this on the DataContext.

这很可能发生,因为默认超时是 30 秒。您可以通过在连接字符串上设置 Timeout 属性来更改此设置。或者,您可以在DataContext上设置它。

Here's an articleon code project about this.

这是一篇关于代码项目的文章

回答by KM.

increase timeout = BAD
fix query = GOOD

增加超时 =错误
修复查询 = GOOD

I'm not sure of all the details involved, but in general the followng applies:

我不确定所涉及的所有细节,但一般情况下以下适用:

when a query runs slow do the following in management studio:

当查询运行缓慢时,请在管理工作室中执行以下操作:

  • run SET ShowPlan_All ON
  • run your query
  • look at the output for the word "scan". your problem is there.
  • 运行 SET ShowPlan_All ON
  • 运行您的查询
  • 查看单词“scan”的输出。你的问题就在那里。

"Scan" = touch each row (table or index). Would you like to "scan" a phone book looking for one number or use the index?

“扫描”= 触摸每一行(表格或索引)。您想“扫描”电话簿寻找一个号码还是使用索引?