[C#.net]Connection Timeout和Command Timeout

每次对数据库连接时,我们有时候会碰到连接超时或者命令超时,这两个超时是不一样的。以ADO.NET为例,当客户端和服务器端连接时,碰到的超时情况主要有下面几种:

当从连接池获取一个连接时,碰到超时。

当建立一个全新连接(而不是从连接池获取)时,碰到超时。

当发送一个命令(command)到SQL Server时,超时。

当发送命令(连接字符串带有“context connection=true”属性)到SQL Server时,超时。

当不是显示的发送一个命令(implicitly)到SQL Server时,碰到超时。

当执行异步命令时,(BeginExecute)碰到超时。

当从服务器端,获取行时,碰到超时。

当用Bulk copy方式,上传数据时,碰到超时。

这些超时主要是通过连接字符串中的Connect Timeout和SqlCommand.CommandTimeout来进行控制。前面两种是登录超时由Connection Timeout来决定什么时候超时,后面几种是命令超时由Command Timeout来决定什么时候超时。

SqlConnection.ConnectionTimeout : 
1. 说明: 获取在尝试建立连接时终止尝试并生成错误之前所等待的时间。
2. 默认值:等待连接打开的时间(以秒为单位)。默认值为 15 秒。
3. 备注:值 0 指示无限制
4. 只读

SqlCommand.CommandTimeout 
1. 说明:获取或设置在终止执行命令的尝试并生成错误之前的等待时间。
2. 默认值:等待命令执行的时间(以秒为单位)。默认为 30 秒。
3. 备注:值 0 指示无限制,在 CommandTimeout 中应避免值 0,否则会无限期地等待执行命令。
4. 可读可写

特别注意:
"超时时间已到。在操作完成之前超时时间已过或服务器未响应"
类似这种错误,一般是 SqlCommand.CommandTimeout  或者 SqlBulkCopy.BulkCopyTimeout 的时间超时, 而不是 SqlConnection.ConnectionTimeout。

原文地址:https://www.cnblogs.com/masonlu/p/9494456.html

时间: 2024-08-03 13:27:27

[C#.net]Connection Timeout和Command Timeout的相关文章

Connection Timeout和Command Timeout

原文:Connection Timeout和Command Timeout 每次对数据库连接时,我们有时候会碰到连接超时或者命令超时,这两个超时是不一样的.以ADO.NET为例,当客户端和服务器端连接时,碰到的超时情况主要有下面几种: 当从连接池获取一个连接时,碰到超时. 当建立一个全新连接(而不是从连接池获取)时,碰到超时. 当发送一个命令(command)到SQL Server时,超时. 当发送命令(连接字符串带有"context connection=true"属性)到SQL S

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

今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" 或者 SqlClient Class , 你在执行一个很费时的SQL 操作时候,可能就会碰到下面的超时异常. --------------------------- ---------------------------Timeout expired.  The timeout period elapsed

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

今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" 或者 SqlClient Class , 你在执行一个很费时的SQL 操作时候,可能就会碰到下面的超时异常. --------------------------- ---------------------------Timeout expired.  The timeout period elapsed

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

今天遇到了Reporting Services(SQL SERVER 2008 R2)的报表执行异常情况,报表加载数据很长时间都没有响应,最后报"An error occurred within the report server database.  This may be due to a connection failure, timeout or low disk condition within the database.;".检查C:\Program Files\Micro

postgreSql 报错 (psycopg2.errors.IdleInTransactionSessionTimeout) terminating connection due to idle-in-transaction timeout 解决

具体问题: 第二个问题呢就是 原因大概是酱紫的 在mysql超过wait_timeout后,连接会挂掉,服务就会报错 OperationalError: (_mysql_exceptions.OperationalError) (2006, 'MySQL server has gone away') ,由于也sqlalchemy关闭了autocommit,隐式使用了事务,还会有错误StatementError: (sqlalchemy.exc.InvalidRequestError) Can't

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.

1.Max Pool Size 默认为100. 2.查看当前数据库连接数量, select * from sysprocesses where dbid= db_id('数据库名称') 结果为190, 3.添加数据库查询的最大连接数量 Data Source=.;Initial Catalog=i_Master;Persist Security Info=True;User ID=NUser;Password=N2;max pool size=500设置最大连接数量 500 原文地址:https

Timeout expired. The timeout period elapsed prior to completion of the operation or the server...

问题: 最近程序总会抛出如下异常: An exception of type'System.Data.SqlClient.SqlException' occurred and was caught. ---------------------------------------------------------------------------------- Type :System.Data.SqlClient.SqlException, System.Data, Version=4.0.

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

写在前面 在mysql中这个异常是非常常见的,超时分为连接超时和执行超时,而连接超时,大部分原因是网络问题,或客户端到服务端的端口问题造成. bug场景 有的时候,使用MySqlDataReader在读取数据的时候,datareader的特性是在在线读取,而且是一条一条往下读,也就是只进式读取.如果数据非常多,就会造成超时的问题. 这种超时是执行超时引起的. SqlCommand.CommandTimeout 获取或设置在终止执行命令的尝试并生成错误之前的等待时间. 等待命令执行的时间(以秒为单

MS SQL Server 数据库连接字符串

MS SQL Server 数据库连接字符串详解 C# 连接串设置超时时间 SqlConnection.ConnectionTimeout 与 SqlCommand.CommandTimeout 及SqlBulkCopy.BulkCopyTimeout属性 https://msdn.microsoft.com/zh-cn/library/8xx3tyca.aspx  SQL Server 连接池 (ADO.NET) http://www.docin.com/p-576271306.html 4.