0

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.

2
Contributors
2
Replies
10
Views
7 Years
Discussion Span
Last Post by Krstevski
0

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.

0

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 by Krstevski: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.