0
我一直在試圖檢查數據庫是否可訪問並根據連接狀態執行自定義操作。我的代碼如下SqlServer連接字符串中的連接超時屬性不工作
string connectionString = "Data Source =" + textBox1.Text + ";Initial Catalog=master;Integrated Security=true;Connect Timeout=1";
SqlConnection connection = new SqlConnection(connectionString);
try
{
connection.Open();
//do some action
connection.Close();
}
catch (SqlException)
{
connection.Close();
//do some action
}
我通過一個TextBox提供目標機器名稱。在上面的代碼中,只要連接成功,try塊內的代碼將在幾秒鐘內執行。但是如果無法建立連接,它將忽略連接超時屬性並等待30秒鐘,然後移至catch塊。
我試了下面的鏈接connection timeout property in connection string ignored,發現沒有成功。我嘗試了5,10,15和20秒的連接超時值,似乎沒有任何工作。
如果我的方法出現問題,請幫我解決。
謝謝!