简体   繁体   中英

Can a database be created in C# using OLEDB and nothing else?

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. XX 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.

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");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM