更改连接字符串中的超时时间
更改连接字符串中的超时时间
1. 连接字符串指定Connection TimeOut数值。
2. 替换连接字符串Connection TimeOut数值。
修改数据库连接超时有以下两种方式:
2. 替换连接字符串Connection TimeOut数值。
以下代码用于替换连接字符串中的超时时间。
C# Code:
/// <summary>
/// 更改连接字符串的超时时间(适用MsSQL,MySQL)
/// </summary>
/// <param name="sConn">连接字符串</param>
/// <param name="TIME_OUT">超时时间,秒</param>
internal static void SetTimeOut(ref string sConn, int TIME_OUT)
{
if (sConn.ToLower().IndexOf("Connection TimeOut".ToLower(), 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.ToLower().IndexOf("Connection TimeOut".ToLower(), 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:23404761
/// <summary>
/// 更改连接字符串的超时时间(适用MsSQL,MySQL)
/// </summary>
/// <param name="sConn">连接字符串</param>
/// <param name="TIME_OUT">超时时间,秒</param>
internal static void SetTimeOut(ref string sConn, int TIME_OUT)
{
if (sConn.ToLower().IndexOf("Connection TimeOut".ToLower(), 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.ToLower().IndexOf("Connection TimeOut".ToLower(), 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:23404761
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网