This snippet will allow you to use the OpenIfClosed DBConnection extension method for C# and VB.NET.

Sample C#

public static void OpenIfClosed(this DbConnection connection)
{
	if (connection!=null && connection.ConnectionString!=String.Empty && connection.State == ConnectionState.Closed)
	{
		connection.Open();
	}
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Sub OpenIfClosed(connection As DbConnection)
	If connection IsNot Nothing AndAlso connection.ConnectionString <> String.Empty AndAlso connection.State = ConnectionState.Closed Then
		connection.Open()
	End If
End Sub

One thought on “OpenIfClosed DBConnection extension method for C# and VB.NET”

Leave a Reply