Test sql connection without throwing exception

Posted by Alexandre Pepin on Stack Overflow See other posts from Stack Overflow or by Alexandre Pepin
Published on 2010-04-08T16:06:50Z Indexed on 2010/04/08 17:23 UTC
Read the original article Hit count: 383

Filed under:
|
|

To test if i can connect to my database, I execute the following code :

using (SqlConnection connection = new SqlConnection(myConnectionString))
{
   try
   {
      connection.Open();
      canConnect = true;
   }
   catch (SqlException) { }
}

This works except it throws an exception if the connection failed. Is there any other way to test a Sql connection that doesn't throw an exception ?

Edit : To add precision, i'm asking if there is a simple method that does that without having to open the connection and catch exceptions that can occur

© Stack Overflow or respective owner

Related posts about c#

Related posts about sqlconnection