Hello friends, I have a small problem, I need to check if the database connection is still open. Here is my code...

try {
// new connection
// connection open
// other code
// close the connection
} catch (Exception ex) {
// handling exception
} finally {
// Here I want to close the connection if still open
}

So... how to check id the connection is still open ?
p.s. I use C#.NET 2.0, MS Access database (OleDbConnection).

Thanks.

Yes you can check it with below line of code...

if (yourconnection.State.Equals(ConnectionState.Open))
{
   // perform operation you want
}

that's all..

Let us know if it will not work for you..

Hello friends, I have a small problem, I need to check if the database connection is still open. Here is my code...

try {
// new connection
// connection open
// other code
// close the connection
} catch (Exception ex) {
// handling exception
} finally {
// Here I want to close the connection if still open
}

So... how to check id the connection is still open ?
p.s. I use C#.NET 2.0, MS Access database (OleDbConnection).

Thanks.

Yup, it works, thanks @rohand. :)

If someone else have a similar problem, here is the solution for my problem:

finally
            {
                // this.connection = new OleDbConnection("MS Access conn string"); 
                if(this.connection.State.Equals(System.Data.ConnectionState.Open))
                {
                    this.connection.Close();
                }
            }

Edited 6 Years Ago by Krstevski: n/a

This question has already been answered. Start a new discussion instead.