首先连接超时分为三种,
TCP Connection to SQL Server -> SqlConnection.Open -> SqlCommand.Execute
先说第二种超时,sqlcon打开,我们不能直接设置connectiontimeout,只能在连接字符串中设置:
Data Source=server;Initial Catalog=databaseUser Id=username;Password=password;Connect Timeout=3
此设置默认时间为妙,而不是毫秒。默认是三十秒,可以设置为3秒。
注意:此处不能设置conn.open()超时回应的时间。因为conn.open(),不只是sqlserver响应尝试连接数据库的时间,其中还有Tcp请求的时间.所以如果 
你设置Connect Timeout=2,在连接不到server的情况下,报告超时的时间也远远超过2s。因为此处设置的只是在sqlserver必须回应一个连接 
尝试的时间。但是如果你根本就无法连接到server,这个设置也就无从谈起。

第三种超时比较简单,就是sql语句在数据库中的执行时间。通过 SqlCommand.CommandTimeout就可以进行设置。

第一种超时就是tcp请求的超时,这个是没有办法通过设置属性实现的。但是我们必须要控制它,因为一个连接可能几十秒之后才会回应你超时 
了,这是难以忍受的。

下面是一个例子:
public static class SqlExtensions
{
public static void QuickOpen(this SqlConnection conn, int timeout)
{
// We'll use a Stopwatch here for simplicity. A comparison to a stored DateTime.Now value could also be used
Stopwatch sw = new Stopwatch();
bool connectSuccess = false;

// Try to open the connection, if anything goes wrong, make sure we set connectSuccess = false
Thread t = new Thread(delegate()
{
try
{
sw.Start();
conn.Open();
connectSuccess = true;
}
catch { }
});

// Make sure it's marked as a background thread so it'll get cleaned up automatically
t.IsBackground = true;
t.Start();

// Keep trying to join the thread until we either succeed or the timeout value has been exceeded
while (timeout > sw.ElapsedMilliseconds)
if (t.Join(1))
break;

// If we didn't connect successfully, throw an exception
if (!connectSuccess)
throw new Exception("Timed out while trying to connect.");
}
}

参考链接:http://improve.dk/archive/2008/03/10/controlling-sqlconnection-timeouts.aspx

C# 控制连接超时的更多相关文章

  1. 使用ffmpeg的av_read_frame,如何控制连接超时

    最近使用ffmpeg来做一个rtsp的客户端,这过程也遇到不少问题,不过相应都比较好,一路走下来.不过到项目结尾时,且遇到一个比较纠结的问题.那就是客户端在使用的过程中,把rtsp服务器的网断了.这时 ...

  2. 关于MySQL的wait_timeout连接超时问题报错解决方案

    bug回顾 : 想必大家在用MySQL时都会遇到连接超时的问题,如下图所示: ### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ...

  3. MySQL连接问题【如何解决MySQL连接超时关闭】

    --MySQL连接问题[如何解决MySQL连接超时关闭] ------------------------------------------------转载 最近做网站有一个站要用到WEB网页采集器 ...

  4. HttpClient(二)HttpClient使用Ip代理与处理连接超时

    前言 其实前面写的那一点点东西都是轻轻点水,其实HttpClient还有很多强大的功能: (1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等) (2)支持自动转向 (3)支持 ...

  5. (五)HttpClient 连接超时及读取超时

    第一节: HttpClient 连接超时及读取超时 HttpClient连接超时及读取超时 httpClient在执行具体http请求时候 有一个连接的时间和读取内容的时间: HttpClient连接 ...

  6. 解决ssh连接超时时间(ssh timeout)的设置方法

    本文介绍下,linux中ssh连接超时时间的设置方法,以避免总是被强行退出.有需要的朋友,参考下吧.有关修改ssh连接超时时间的方法,网上介绍的很多了.比如下面这个:可以减少ssh连接超时等待的时间: ...

  7. [PHP]socket的连接超时 与 读取/写入超时

    socket处理时有两种超时 , 分为连接超时 和 读取/写入数据超时 1. stream_socket_client 函数中的超时时间是连接超时 , 默认是php.ini中的default_sock ...

  8. 如何解决MySQL连接超时关闭

    最近做网站有一个站要用到WEB网页采集器功能,当一个PHP脚本在请求URL的时候,可能这个被请求的网页非常慢慢,超过了mysql的 wait-timeout时间,然后当网页内容被抓回来后,准备插入到M ...

  9. Solaris10 如何设置空闲ssh连接超时断开

    在ssh的配置文件中有2个参数可以控制空闲连接超时断开.这2个参数是ClientAliveCountMax和ClientAliveInterval. Solaris10上设置空闲ssh连接超时断开的方 ...

随机推荐

  1. zabbix使用tokudb引擎替换innodb引擎

    zabbix数据量大,数据量增长很快,使用tokudb可以更好的压缩 使用tokudb,用percona 或mariadb数据库 1.查看数据库版本 (1)登录数据库的时候可以看到 (2)status ...

  2. 第九篇 SQL Server安全透明数据加密

    本篇文章是SQL Server安全系列的第九篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...

  3. spring 定时任务标注

    使用spring框架,需要定时任务只需要在方法上加@Component 就可以了 package hello; import java.text.SimpleDateFormat; import ja ...

  4. CALayer 图层

    // CALayer 图层属性,继承UIView都有该属性,可设置边框宽度.颜色.圆角.阴影等 UIImageView *imageView = [[UIImageView alloc]initWit ...

  5. System.Threading.Timer使用心得

    System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...

  6. iOS 顺传

    ios 顺传一层的话,直接用属性 改变里面的值 顺传穿两到三层的话 使用KVO // 设置item - (void)setItem:(UITabBarItem *)item { _item = ite ...

  7. PostgreSQL数据库系统的进程结构

    PostgreSQL数据库系统的主要功能都集中于Postgres程序,其入口是Main模块中的main函数,在初始化数据集簇,启动数据库服务器是,都将从这里开始执行.Main模块主要的工作时确定当前的 ...

  8. Lintcode: Rehashing

    The size of the hash table is not determinate at the very beginning. If the total size of keys is to ...

  9. __int64和long long输入输出

    __int64 num; scanf("%I64d", &num); printf("%I64d\n", num); long long num; sc ...

  10. javascript浏览器对象

    window对象 1.window对象 window对象是BOM的核心,window对象指当前的浏览器窗口 所有JS全局对象.函数以及变量均自动成为window对象的成员 全局变量是window对象的 ...