C# .NET - how to check if the connection is still open?

Asked By jason smith on 21-Jun-06 02:16 PM
I want to check in the finally block to see if the connection is still open.

how can I do that?

thanks
nath

Use Connection State property - Asked By Peter Bromberg on 21-Jun-06 02:24 PM

if (myConnection.State == ConnectionState.Open)
myConnection.Close();

You can call the Close method on a connection that is already closed without throwing an exception.

compiler error - Asked By jason smith on 21-Jun-06 02:49 PM

Operator '==' cannot be applied to operands of type 'System.Data.ConnectionState' and 'method group'
Asked By Sushila Patel on 21-Jun-06 05:37 PM
Can you post your code. Peter's code should work
this is the code Patel - Asked By jason smith on 21-Jun-06 10:13 PM
finally
{
            if (oMSAccessConnection.State == oMSAccessConnection.Open)
             oMSAccessConnection.Close(); 

}

thanks patel

nath
don replied to jason smith on 03-Mar-11 10:23 AM
As I recall you can do this:
  if(connection.State.ToString=="Open") connection.Close();

Andy replied to jason smith on 09-Jun-11 01:16 PM
The same error:
Operator '==' cannot be applied to operands of type 'System.Data.ConnectionState' and 'method group'

Resolved adding:
using System.Data;

Hope someone usefully!
Ahmad replied to jason smith on 20-Jul-11 08:50 AM

sqlconnection conn = new sqlconnection( "YourConnectionString ");
if( conn.state.toString() == "Open")
{
conn.close();
//or do whatever you want .
//dont forget to put .toString() not .toString

}

Best Regards
Kevin LeGris replied to Andy on 15-Nov-11 01:29 PM
Using System.Data;

that did it for me.

I really wish people would remember to include these statements when they suggest code.  It is very frustrating when someone suggests something that works wonderfully, except they forgot to tell you where it is contained.
misko misko replied to jason smith on 17-Apr-12 06:19 AM
use technique which is used by tableadapters, so check:
if (((yourConnection.State & global::System.Data.ConnectionState.Open) 
            != global::System.Data.ConnectionState.Open)) 
{         
	yourConnection.Open();
}
...     public virtual int Delete(global::System.Nullable<long> RecordID) {       if ((RecordID.HasValue == true)) {         this.Adapter.DeleteCommand.Parameters[1].Value = ((long)(RecordID.Value));       }       else {         this.Adapter.DeleteCommand.Parameters[1].Value = global::System.DBNull.Value;       }       global::System.Data.ConnectionState previousConnectionState = this.Adapter.DeleteCommand.Connection.State;       if (((this.Adapter.DeleteCommand.Connection.State & global::System.Data.ConnectionState.Open)              != global::System.Data.ConnectionState.Open)) {         this.Adapter.DeleteCommand.Connection.Open();       }       try {         int returnValue = this.Adapter.DeleteCommand.ExecuteNonQuery();         return returnValue;       }       finally {         if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {           this.Adapter.DeleteCommand.Connection.Close();         }       }     }