ADO.NET: faster way to check if a database server is accessible?

I am currently using this code to check if a database is available:

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;
}

Although this code works fine, there is a flaw. If the server is offline, it spends about 4 full seconds trying to open the connection before deciding that it is unavailable.

Is there a way to test the connection without actually trying to open it and wait for a timeout? Something like basic ping equivalent?

+5
source share
3 answers

( ) , , , , . MS SQL Server:

" Sysdatetime()"

+2

.

, , . , , "" , . , "" . , - , , , , .

, , . , , .

The final option is that some background server constantly monitors the database server, and when it detects that it is disconnected, it notes something appropriately. Then your application will check this thing (actually a cache), and it will be almost immediately. Of course, this would mean that it could give false negatives (that is, the server may be on the network, but the monitoring service has not yet updated the cache).

-1
source

All Articles