0

Using ADOX Catalog class I am trying to create an empty Ms Access Database.The code is working fine(as far as I know) and generates the database but when I try to open the database from windows explorer, I am getting the "Could not use name.accdb ; the file already in use" error message.I checked the file directory and the name.idb file also was appeared on the directory without opening the database. After I close the C# windows form application the name.idb disappear and I can open the database. Here is the code which I used to create the database using ADOX.

 public void CreateDatabase()
    {
        string databaseName = txtFileName.Text;
        try
        {
            ADOX.Catalog cat = new ADOX.Catalog();

            if (!File.Exists(databaseName + ".accdb"))
            {
                cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data 
                 Source=D:\\" + databaseName + ".accdb" + ";");
                cat = null;
            }
            cat = null;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, e.Source);
        }
    }

I also tried to dispose the cat object by implementing IDispaosable interface but nothing changed. Can you please let me know how I can fix this issue which user can open database after generating by the application without closing (Killing)the app?

Regards

Suffii
  • 5,694
  • 15
  • 55
  • 92

1 Answers1

3

Perhaps you need to release the object. Try adding the following after you call Create:

Marshal.FinalReleaseComObject(cat.Tables);
Marshal.FinalReleaseComObject(cat.ActiveConnection);
Marshal.FinalReleaseComObject(cat);
John Straka
  • 1,874
  • 5
  • 28
  • 49