当前位置: 首页 > 技术分享  > 开发技术 > 数据库应用

SQL Timeout expired问题的解决

2015/4/30 10:24:32 人评论

系统报错:“The timeout period elapsed prior to completion of the operation or the server is not responding”,用查询分析器执行…

系统报错:“The timeout period elapsed prior to completion of the operation or the server is not responding”,用查询分析器执行存储过程需要16分左右,但是.net中一运行,不到30秒就报错,可见不是存储过程有问题,肯定是.net本身有个连接时间限制问题。


如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" 或者 SqlClient Class , 你在执行一个很费时的SQL 操作时候,可能就会碰到下面的超时异常。"Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding."
你会说,我在连接字符串中已经设置了 Connect Timeout=80000 ,并且数据库中超时连接也是设置的值是一个很大的值。为啥到了30秒,仍然超时了呢??

这是因为:
你的设置并没有问题,是你混淆了  SqlCommand.CommandTimeout  和 SqlConnection.ConnectionTimeout 这两个的区别了。
你的连接字符串中的超时只是设置的 SqlConnection.ConnectionTimeout 的值,而不是设置的 SqlCommand.CommandTimeout 的值。SqlHelper 中并没有 SqlCommand.CommandTimeout 的相关设置。需要你自己设置。

下面是两个的比较:

SqlCommand.CommandTimeout
获取或设置在终止执行命令的尝试并生成错误之前的等待时间。
等待命令执行的时间(以秒为单位)。默认为 30 秒。

SqlConnection.ConnectionTimeout
获取在尝试建立连接时终止尝试并生成错误之前所等待的时间。
等待连接打开的时间(以秒为单位)。默认值为 15 秒。

http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=357 关于Timeout  expired问题的解决原文 


"The timeout period elapsed prior to completion of the operation or the server is not responding."
If this sounds familiar using the SqlClient Class or the Data Access Application Blocks "SqlHelper", and you have increased the Connection Timeout and the Connect Lifetime (for pooling) in your connection string(s), its probably because you forgot to set the COMMAND timeout!
A command can be timed out after a certain number of seconds. You might want to set this limit if you foresee to run across particularly lengthy operations. As in ADO, the property to check is CommandTimeout. Its default value is 30 seconds.
You can set this once the command instance has been created. A value of "0" (zero) means the command will wait for completion indefinitely, but this is not recommended by Microsoft. Better to set a large value in seconds.
Unlike ADO, ADO.NET lets you specify the expected behavior of the command through the CommandBehavior enum. Such values specify a description of the results and how the query should affect the data source. In Beta 1, you had a CommandBehavior property to set for each command. Starting with Beta 2, you use values from the CommandBehavior enum only as an argument for ExecuteReader.
Among the other options, you can ask a query command to limit to obtain key and schema information. In this case, the command will be executed without any locking on the selected rows. This behavior is given by the KeyInfo flag. If you have long running queries or multiple threads accessing Sql Server simultaneously, this can be very helpful.
As an alternative, you might want to obtain column information only, without affecting the database state with locks. This option is SchemaOnly. Another option, SingleResult, lets you specify that you want back only one resultset, no matter how many would originate from the command. In this case, the command returns only the first resultset found. A fourth option is CloseConnection that forces the SqlDataReader object associated with a query command to automatically close the connection as the final step of its Close method.
If you use the SqlHelper "Best practices" class as I do, it might be a good idea to recompile it, setting "cmd.CommandTimeout=howmanyseconds. There are a number of instances of this in the class.
And, as a final caveat, don't call Dispose() on a SqlConnection unless you want to have it removed from the connection pool, because that's what Dispose() does! In almost all cases, you would simple call the Close() method and let ADO.NET take care of returning the connection to the pool.

相关技术

  • MSSQL2016 SSMS18 闪退解决方法

    服务器最近安装MSSQL 2016,安装包中没有集成Microsoft SQL Server Management Studio。需要另外下载,官网找到SSMS-Setup-CHS.exe,选择15.0.18118.0版本。安装完后,双击桌面 Microsoft SQL Server Management Studio 18 无响应,一闪而过。使用管理员权限,问题依旧。经…

    2019/6/10 22:30:33
  • SQL Server 数据库定时自动备份

    在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库。而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员 每天守到晚上1点去备份数据库。要实现数据库的定时自动备份,最常用的方式就是使用SQL Server代理中的作业。启动S…

    2018/6/24 10:29:38
  • 4种C#SQL语句写法

    记录4种C#SQL语句写法,个人比较喜欢用第二种string.Format方法;(1)拼接产生SQL语句: string sql = "insert into czyb(yhm,mm,qx) values(" + txtName.Text + "," + txtPassword.Text + "," + cmbPriority.Text + ")";…

    2018/2/1 12:15:34
  • MSSQL游标CURSOR应用实例

    我们使用SQL语句处理数据时,可能会碰到一些需要循环遍历某个表并对其进行相应的操作(添加、修改、删除),这时我们就需要用到咱们在编程中常常用的for或foreach,但是在SQL中写循环往往显得那么吃力,翻遍网上的资料,找不到几个正确的并能执行的循环处理数据的方法,在…

    2017/12/8 8:38:13

共有条评论 网友评论

验证码: 看不清楚?