c# - What's the best way to test SQL Server connection programmatically? - Stack Overflow

PHOTO EMBED

Sat Feb 26 2022 17:33:30 GMT+0000 (Coordinated Universal Time)

Saved by @paulbarry

/// <summary>
/// Test that the server is connected
/// </summary>
/// <param name="connectionString">The connection string</param>
/// <returns>true if the connection is opened</returns>
private static bool IsServerConnected(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        try
        {
            connection.Open();
            return true;
        }
        catch (SqlException)
        {
            return false;
        }
    }
}
content_copyCOPY

https://stackoverflow.com/questions/2440060/whats-the-best-way-to-test-sql-server-connection-programmatically