ADO.NET: Faster way to check if the database server is accessible?

Posted by lotana on Stack Overflow See other posts from Stack Overflow or by lotana
Published on 2010-05-04T00:13:33Z Indexed on 2010/05/04 0:18 UTC
Read the original article Hit count: 269

Filed under:
|
|
|

At the moment I am using this code to check if the database is accessible:

public bool IsDatabaseOnline(string con) { bool isConnected = false; SQLConnection connect = null;

try {
    connect = new SQLConnection(con);
    connect.Open();
    isConnected = true;

} catch (Exception e) {
    isConnected = false;

} finally {
    if (connect != null)
        connect.Close();
}

return isConnected;

}

While this code works fine, there is a disadvantage. If the server is not online it spends about 4 full seconds trying to open the connection before deciding that it is not available.

Is there a way to test the connection without trying to actually opening it and waiting for the timeout? Something like a database-equivalent of ping?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ADO.NET