SQL Timeout超时的处理方法

第一步:修改Web.config配置文件。在数据库连接字符串中加上连接时间Connect Timeout,根据实际情况定时间。

  1. <!--连接数据库-->  
  2. <connectionStrings>  
  3.      <add name="strConnDB" connectionString=" Data Source=192.168.*.*;Initial Catalog=DatabaseName;Persist Security Info=True;User id=sa;Password=password;pooling=true;max pool size=800;min pool size=300;<span style="color:#FF0000;">Connect Timeout=500</span>;"/>  
  4. </connectionStrings>  


第二步:修改command对象的CommandTimeout属性。 

  1. SqlCommand cmd = new SqlCommand();  
  2. cmd.CommandTimeout = 180;  

这里设置的时间是180秒,即三分钟!可根据需要设置,如果过长,也可以设置为0,当此属性设置为0时表示不限制时间。此属性值应该慎用。

到此为止,问题完美解决。

补充:
SqlCommand.CommandTimeOut:获取或设置在终止执行命令的尝试并生成错误之前的等待时间。
SqlConnection.ConnectionTimeout:获取在尝试建立连接时终止尝试并生成错误之前所等待的时间。

原文地址:https://www.cnblogs.com/lenther2002/p/6840845.html