更改连接字符串中的超时时间
作者:C/S框架网  发布日期:2013-07-20 22:04:44
  更改连接字符串中的超时时间


1. 建立SQLConnection实例,修改.TimeOut属性
2. 某种特殊情况,直接替换字符串的超时数值


C# Code:

/// <summary>
/// 更改连接字符串中的超时时间
/// </summary>
/// <param name="sConn">连接字符串</param>
/// <param name="TIME_OUT">超时时间,秒</param>
public void SetTimeOut(ref string sConn, int TIME_OUT)
{
   if (sConn.ToUpper().IndexOf("Connection TimeOut".ToUpper(), 0) <= 0)
   {
      sConn = sConn ";Connection TimeOut=" TIME_OUT.ToString() ";";
   }
   else
   {
      string[] items = sConn.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
      for (int i = 0; i <= items.Length - 1; i )
      {
         string s = items[i];
         //;Connection TimeOut={4};
         if (s.IndexOf("Connection TimeOut".ToUpper(), 0) > 0)
         {
            string[] t = s.Split('=');
            items[i] = "Connection TimeOut=" TIME_OUT.ToString();
         }
      }
      StringBuilder sb = new StringBuilder();
      foreach (string s in items) sb.Append(s ";");
      sConn = sb.ToString();
   }
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898


上一篇 下一篇