IDbConnection - IsNotConnectionOpen

A DbConnection extension method that queries if a not connection is open.

Try it

public static void Main()
{		
	using (var conn = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
    {
		//C# Extension Method: IDbConnection - IsNotConnectionOpen
        var result1 = conn.IsNotConnectionOpen(); // return true
		
        conn.EnsureOpen();
		
		//C# Extension Method: IDbConnection - IsNotConnectionOpen
        var result2 = conn.IsNotConnectionOpen(); // return false
		
		Console.WriteLine(result1);
		Console.WriteLine(result2);
    }
}

View Source
using System.Data;
using System.Data.Common;

public static partial class Extensions
{
    /// <summary>A DbConnection extension method that queries if a not connection is open.</summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>true if a not connection is open, false if not.</returns>
    public static bool IsNotConnectionOpen(this DbConnection @this)
    {
        return @this.State != ConnectionState.Open;
    }
}