How to check for database availability

SqlConnection will throw a SqlException when it cannot connect to the server.

public static class SqlExtensions
{
    public static bool IsAvailable(this SqlConnection connection)
    {
        try
        {
            connection.Open();
            connection.Close();
        }
        catch(SqlException)
        {
            return false;
        }

        return true;
    }
}

Usage:

using(SqlConnection connection = GetConnection())
{
    if(connection.IsAvailable())
    {
        // Success
    }
}

You can try like this.

    public bool IsServerConnected()
    {
        using (var l_oConnection = new SqlConnection(DBConnection.ConnectionString))
        {
            try
            {
                l_oConnection.Open();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
        }
    }

Your code seems fine, but you really need to use the IDisposable pattern, and some naming convention too:

private bool CheckDbConnection(string connectionString)
{
    try
    {
        using(var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            return true;
        }
    }
    catch (Exception ex)
    {
        logger.Warn(LogTopicEnum.Agent, "Error in DB connection test on CheckDBConnection", ex);
        return false; // any error is considered as db connection error for now
    }
}

And connection.Close() is not supposed to throw. Just use the using block and your are fine.

No need to test the Close state, since you have just opened it.
More about the Broken state:

Broken The connection to the data source is broken. This can occur only after the connection has been opened. A connection in this state may be closed and then re-opened. (This value is reserved for future versions of the product.)

So really, no need to test that.

The Connecting state could be catch if you are in a multithread context and your instance of connection is shared. But it is not your case here.


This code does not block a UI if called.

public static class DatabaseExtensions
{
    public static async Task<bool> IsConnectionViable(this string connectionStr)
    {
        await using var sqlConn = new SqlConnection(connectionStr);
        return await sqlConn.IsConnectionViable();
    }

    public static async Task<bool> IsConnectionViable(this SqlConnection connection)
    {
        var isConnected = false;

        try
        {
            await connection.OpenAsync();
            isConnected = (connection.State == ConnectionState.Open);
        }
        catch (Exception)
        {
            // ignored
        }

        return isConnected;
    }
}