1

I've been using OLEDB programmatically (No References in Visual Studio, just accessing the database, which is in the same folder as the program, through code) to access and modify Microsoft Access databases in Visual Basic. I've always made sure my programs have any required databases already created and packaged with the program, but I have been porting some of my VB programs to C# and was wondering if it was possible to create a database file if the database is lost or deleted.

I've seen a few tutorials on using adding a COM reference to Microsoft ADO Ext. X.X for DLL and SecurityADOX, but I was wondering if there was a way to use OLEDB to create and then instantiate the database.

Something along the lines of checking if the database exists

if (!File.Exists("Database.accdb"))

then some code that isn't ADOX's Catalog.Create (I've also seen CatalogClass.Create) but is an OLEDB equivalent, if there is one.

I've read a couple Stack Overflow questions which all say to use ADOX. Even Microsoft's tutorials involve ADOX.

I'm completely open to adding ADOX and still using OLEDB if that's the best course, or even switching to SQLite if it's faster/more efficient, but I was just asking if there's an OLEDB-only way.

Community
  • 1
  • 1
pfthroaway
  • 85
  • 1
  • 8
  • 1
    The accepted answer in one of the questions you linked to says _"The simplest answer is to embed an empty .mdb / .accdb file in your program and write it out to disk._" – stuartd Oct 06 '16 at 17:07
  • And Microsoft has a [great tutorial](https://support.microsoft.com/en-us/kb/319292) on how to do that. It's a great solution. However, the question is: is there a way to create a database file that doesn't exist simply using OLEDB? Based on the lack of answers I've found using Google, the answer to the question is no, but I wanted to ask anyway. I appreciate you taking the time to make this comment. – pfthroaway Oct 06 '16 at 17:16

1 Answers1

-1
public void CreateAccessdatabase()
    {
        try
        {
            ADOX.Catalog cat = new ADOX.Catalog();
            cat.Create("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=" + SaveDialogTargetFile.FileName + ";;Jet OLEDB:Engine Type=5;");
            cat = null;

            TxtDatabaseFile.Text = SaveDialogTargetFile.FileName;

            string CreatedDatabaseFileName = Path.GetFileNameWithoutExtension(SaveDialogTargetFile.FileName);
            TreeViewDatabaseScheme.Nodes.Add(CreatedDatabaseFileName);
            MessageBox.Show("Databasefile succesfully created");

        }
        catch (Exception)
        {
            MessageBox.Show("Failed to create new databasefile");
        }
    }
  • I use this to create the database, may not fully respond to what you are asking. I'm currently writing code to create new tables from code – Pieter Romeyns Dec 30 '18 at 18:56