Connection closed

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Using ASP.Net to read/write to an Access DB on a webserver.
How can I check to make sure I haven't left any connections open ? I
have physical access to the server if I need to check it there. I really
want to verify all is well before I go live with it.

Thanks,
Mark
 
The connection object has a ConnectionState property (or something to that
effect). It can be used to determine if the connection is open or not.

If you are not sure, you can call the Close method (and you should be doing
this when your code is done with the connection) a second time. It will not
throw an exception even if the connection was already closed.

Also, be aware that ADO.NET connections use connection pooling, so even
though your connection may be closed it will still exist (in a closed state)
in the pool in case some other bit of code needs to use it again.
 
Apart from the Scott's good reply I would suggest you to embedd Open/Close
into try/finally statement.

conn.Open();
try
{
...
}
finally
{
conn.Close();
}

Also, connection implements IDisposable, thus you might call it at the end
(even if it does nothing - it could do something in the future).
Btw, Dispose also closes connection:

using (OleDbConnection conn = new OleDbConnection(...))
{
}

Is enough to sleep well.
 
Back
Top