I have to save data and I have to test connection before to save it. How can I test that this connection string is valid for a particular connection?
My code is like this:
static public bool TestConnString(string connectionString)
{
bool returnVal = true;
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();
if (conn.State != ConnectionState.Open)
returnVal = false;
else
returnVal = true;
}
catch (Exception ex)
{
returnVal = false;
}
}
return returnVal;
}
Connection string is:
Data Source=testSvr03\SQLEXPRESS;Initial Catalog=Test; Connection Timeout=600; Persist Security Info=True;User ID=Test; password=test
If I give wrong data source in connection String then it never returns in this function after conn.open() .I put catch block but it is coming in it
Can anyone Tell me what is solution?