I have been trying to Check if the Database is reachable and do custom actions according to the Connection state. My code is as follows
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
}
I provide the Target machine name through a TextBox. In the above code whenever a connection is successfully made, the code within the try block executes within seconds. But if the connection can't be made, it ignores the Connect Timeout property and waits for 30 seconds before moving to the catch block.
I tried the following link connection timeout property in connection string ignored and found no success. I have tried for a Connect Timeout value of 5,10,15 and 20 seconds and nothing seems to work.
Help me out if something is wrong with my approach.
Thanks!