ADO.NET连接时间限制即 connection.Open();的异常时间限制,需配置文件的连接字符串中添加超时设置Connect Timeout =1,应用程序数据库连接不上时,连接打开的时间也限制为1s,否则网络异常时打开数据库的代码会非常耗时,默认为15s。此值可以从代码connection.ConnectionTimeout获得,代码中此属性为只读属性,不可设置,需要在连接的配置文件中设置连接时间。
<DBConnect>
<add key="DatabaseConnection" value="server=192.1.0.2/SQLEXPRESS; database = Menu;uid= sa;pwd = 123;Connect Timeout =1" />
</DBConnect>
读取配置文件主要代码:
XmlDocument xDoc;
xDoc = new XmlDocument();
string path;
path = "RelateProfile.xml";
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//DBConnect");
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='DatabaseConnection']");
if( xElem != null)
{
return xElem.GetAttribute("value");
}
else
{
return "";
}