Check If Database connection is available or not

Madhu.b.rokkam
Posted by Madhu.b.rokkam under C# category on | Points: 40 | Views : 3277
Add this simple function and use to check

public static bool GetConnectionState()
{
try
{
using (SqlConnection con = new SqlConnection(YOUR_SQLCONNECTION_STRING) //Set your connection string here
{
con.Open();
return (con.State == ConnectionState.Open);
}
}
catch (SqlException)
{
return false;
}
catch (Exception ex)
{
return false;
}
}

Comments or Responses

Login to post response