今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者

如果你使用的数据库连接类是 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.
---------------------------
OK   
---------------------------

你会说,我在连接字符串中已经 设置了 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

这个问题可以算是 SqlHelper 设计的时候,一个考虑不周的地方吧。
SqlCommand.CommandTimeout 的默认值是30,对于我写的大多数程序来说,这个值足够了。所以一直都没有发现SqlHelper的这个问题。今天在查本地一台比较差的机子上生成一个超长帖子(近4000个回复)无响应的问题时候,才发现SQLHelper 存在的这个问题。

把command的Timeout属性设置一下就ok了!

 /// <summary>
    /// 执行查询语句,返回DataTable
    /// </summary>
    /// <param name="SQLString">查询语句</param>
    /// <param name="commTime">设置查询Timeout</param>
    /// <returns>用于复杂查询</returns>
    public static DataTable GetDataTable(string SQLString,int commTime)
    {
        string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionString"];
        using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
        {
            DataTable dt = new DataTable();
            try
            {
                connection.Open();
                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
                System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(SQLString, connection);
                comm.CommandTimeout = commTime;
                da.SelectCommand = comm;
                da.Fill(dt);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }
    }

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.【转】的更多相关文章

  1. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

    今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" ...

  2. 记录一次在生成数据库服务器上出现The timeout period elapsed prior to completion of the operation or the server is not responding.和Exception has been thrown by the target of an invocation的解决办法

    记一次查询超时的解决方案The timeout period elapsed...... https://www.cnblogs.com/wyt007/p/9274613.html Exception ...

  3. 解决 Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. 的问题

    在web 网站开发中,经常需要连接数据库,有时候会出现这样的数据连接异常消息: 主要原因是 应用程序与数据库的连接超出了数据库连接的默认时长,在这种情况下,我们可以把数据库连接的时长延长一些,因为 C ...

  4. [bug]Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding

    写在前面 在mysql中这个异常是非常常见的,超时分为连接超时和执行超时,而连接超时,大部分原因是网络问题,或客户端到服务端的端口问题造成. bug场景 有的时候,使用MySqlDataReader在 ...

  5. The timeout period elapsed prior to completion of the operation or the server is not responding.

    问题:更新数据的状态值时,部分报出如下异常: 即时有成功更新,时有报错问题出现. 在LOG中发现成功更新的数据,存在更新时间过长问题,将近30秒(EF默认的CommandTimeout为30秒): 代 ...

  6. SSRS 2008 R2 错误:Timeout expired. The timeout period

    今天遇到了Reporting Services(SQL SERVER 2008 R2)的报表执行异常情况,报表加载数据很长时间都没有响应,最后报"An error occurred with ...

  7. SQLSERVER:Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

    背景: 在最近开发中遇到一个问题,对一个数据库进行操作时,我采用64个并行的任务每个任务保证一个数据库连接对象:但是每个任务内部均包含有24个文件需要读取,在读取文件之后,我们需要快速将这24个文件批 ...

  8. Connection open error . Connection Timeout Expired. The timeout period elapsed during the post-login phase.

    是这样的,最近我在开发Api(重构),用的数据库是Sqlserver,使用的Orm是 SqlSugar(别问我为什么选这个,boss选的同时我也想支持国人写的东西,且文档也很全). 被催的是,写好了程 ...

  9. 网站错误记录:Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.

    今天看公司项目的错误日志文件,发现日志文件都是记录的这个错误. 经过网站查找,发现英文翻译是: 译:超时,与连接池的连接时间已过.这种情况发生是因为连接池在使用和最大连接池数目已满 通过翻译,可以看出 ...

随机推荐

  1. bulk insert 在mssql中使用

    从远程数据文件中批量导入 若要使用 BULK INSERT 从其他计算机中大容量导入数据,必须在两台计算机之间共享数据文件. 指定共享数据文件时,请使用它的通用命名约定 (UNC) 名称,其一般形式为 ...

  2. 每日英语:The Toy Mogul Who Became A Billionaire Through His Fight To The Death With Barbie

    The trophy case in the tiny conference room outside Isaac Larian’s corner office spans an entire wal ...

  3. 运行Virtualbox去安装系统时出错:Failed to open a session for the virtual machine,Unable to load R3 module xxxx/VBoxDD.DLL(VBoxDD)

    貌似看到不少人(1,2,3),在使用Virtualbox去安装系统时,都遇到这个错误: 比如这里遇到的: 截图: 和另外这里的截图: 加文字描述: Failed to open a session f ...

  4. 【Socket】Socket网络编程常用的结构及函数小结

    名词解析 IP地址的作用是标示计算机的网卡地址,每台计算机都有一个IP地址: 端口是指计算机中为了标示在计算机中访问网络的不同程序而设的编号,并不是网卡接线的端口,而是不同程序的逻辑编号,并不是实际存 ...

  5. html5的拖拽dragAPI(如果看了API不懂,看看那三个案例就会恍然大悟)

    1.拖拽和释放定义: 拖拽:Drag; 释放:Drop; 2.源对象和目标对象: 3.相关API: ondragstart:源对象开始被拖动 ondrag:源对象被拖动的过程中 ondragend:源 ...

  6. firefox快捷键窗口和标签类

    firefox快捷键窗口和标签类: 关闭标签: Ctrl+W 或 Ctrl+F4关闭窗口: Ctrl+Shift+W 或 Alt+F4向左移动标签: Ctrl+左方向键 或 Ctrl+上方向键向右移动 ...

  7. ActiveMQ入门实例(转)

    转载自:http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html 1.下载ActiveMQ 去官方网站下载:http://act ...

  8. Web基础 HTML CSS JS JQuery AJAX

    1. Web基础 网页的骨骼HTML 什么是HTML超文本标记语言:Hyper Text Markup Language 这都不重要,重要的是:HTML是Web网页的基本组成部分HTML中定义的元素, ...

  9. SQL 增删改查

    create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default \'默认值\' null , ...

  10. BMP位图文件格式详解及编程建议

    BMP文件渊源流长,虽然对JPG.PNG等格式图像文件来说,确实有点土,但是毕竟BMP文件格式相对简单,容易理解,至于BMP众多的位图格式也不能责怪微软,主要是早期谁也没料到图片技术会发展的这么快,而 ...