Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
  Tip: Use ADOX to make a database without leaving the database files locked  
 
 

This tip comes from Adam Kelly who says:

This drove me nuts for hours so I am passing the tip on.....

If you use ADOX to make a database with the following code snippet then .net leaves the database connection open and a .ldb file remains. That means regardless you cannot compact and repair the database or do ANYTHING that requires exclusive access to the database! Very annoying!

    Friend Sub MakeDB(ByVal strDBPathAndName As String)
        Dim cat As ADOX.Catalog

        Cat = New ADOX.Catalog
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strDBPathAndName & ";")
        cat = Nothing
    End Sub

Here is the fix, add the line:

    CType(cat.ActiveConnection, ADODB.Connection).Close()

SO my question is WHY do I have to cast a connection "close" to the catalog Active connection? Why is there not a cat.ActiveConnection.close OR better yet cat.Close?????? This is at the least bad design by MS or worse a bug!

    Friend Sub MakeDB(ByVal strDBPathAndName As String)
        Dim cat As ADOX.Catalog

        Cat = New ADOX.Catalog
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strDBPathAndName & ";")
        CType(cat.ActiveConnection, ADODB.Connection).Close()
        cat = Nothing
    End Sub

Bottom line if you ever use a ADOX.Catalog then make sure you close it after use as outlined above or you will leave a pool of open connections to the database!

 

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated