Closing Connection

  • Thread starter Thread starter MSNEWS.MICROSOFT.COM
  • Start date Start date
M

MSNEWS.MICROSOFT.COM

I've a OleDBConnection to a Microsoft Access Database. However when I call
the Close method to close the connection to the database, and then doing
something with the database file, it says the database file is still in use.
It seems that it happens not only with Access but other data source as well.
Calling dispose with the connection object doesn't help!
Any help would be appreciated!
 
ADO.NET uses connection pooling so even when your connection falls out of
scope, it's not destroyed (even if you call dispose as well). To disable
connection pooling add this to your connection string:

OLE DB Services=-4;

Also, depending on how you are using your connection's, they may exist in a
different thread from the main application, in which case, you may need to
kill the thread the connection is using as well.
 
Just to touch upon what scott m said... Make sure the connection pooling is
off b/c this is the most likely cause. However, are you positive that the
connection is closed? Have you verified that each connection is closed? If
you are manually opening your connections and an exception is thrown (and
you don't have a finally block), the code calling the close method may be
skipped over altogether

Do you have your data access code centralized so it's easy to track or are
they all over the place? Just to be safe... add a
Debug.Assert(myConnection.State == ConnectionState.Closed); at the end of
each method which opens a connection. If you see a big ugly assertion box
pop up, then something isn't closing properly. If your code is centralized
this shoudl be pretty easy, but it's always worth doing to ensure that your
code is closing the connections and when developing, I assert everything
that I make an assumption about just to be safe.

Also, is this thread single threaded?


HTH,

Bill
 
And to add one more thing, you can call the close method even on a closed
connection (this will not throw an exception), so it doesn't hurt to close
your connection more than once in your code.
 
Back
Top