【发布时间】:2015-10-19 23:49:56
【问题描述】:
我有一个这样的 oledbconnection 尝试捕获:
try
{
OleDbConnection Connection;
using (Connection = new OleDbConnection("Provider=MSDAORA.1;Data Source=DATABASE:1521/orcl//orcl1;Persist Security Info=True;Password=PASS;User ID=USER"))
{
Connection.Open();
v1 = 1;
Connection.Close();
}
}
catch (Exception)
{
v1 = 0;
}
当我无法连接数据库时,尝试捕获并返回v1 = 0。它可以正常工作,但是当连接等待时间过长(例如 30-40 秒)时,请尝试尝试连接和页面等待时间。
我为oledbconnection 尝试了Connect Timeout,但不起作用。
我需要使用try几秒,如果有问题,需要去catch。
我该怎么做?
【问题讨论】:
-
你可以制作命令对象,然后使用OleDBCommand对象的CommandTimeOut属性来设置超时时间,或者设置数据库连接超时时间。
-
你能给我举个例子吗?
-
string sQuery=你的查询在这里 SqlCommand cmd = new SqlCommand(sQuery, _Database.Connection); cmd.CommandTimeout = 0; int countRow = cmd.ExecuteNonQuery();
-
注意:CommandTimeout 设置查询执行超时。
标签: c# asp.net try-catch oledbconnection