简体   繁体   English

可以使用OLEDB在C#中创建数据库吗?

[英]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. 我一直以编程方式使用OLEDB(在Visual Studio中没有引用,只是通过代码访问与程序位于同一文件夹中的数据库)来访问和修改Visual Basic中的Microsoft Access数据库。 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. 我一直确保我的程序已经创建了所有必需的数据库并与该程序一起打包,但是我一直在将某些VB程序移植到C#中,并且想知道如果数据库丢失或丢失,是否可以创建数据库文件?已删除。

I've seen a few tutorials on using adding a COM reference to Microsoft ADO Ext. 我看过一些教程,有关如何使用COM向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. DLL和SecurityADOX为XX,但是我想知道是否存在使用OLEDB创建然后实例化数据库的方法。

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. 那么一些不是ADOX的Catalog.Create代码(我也看到过CatalogClass.Create ),但是与OLEDB等效,如果有的话。

I've read a couple Stack Overflow questions which all say to use ADOX. 我已经阅读了几个Stack Overflow问题, 这些问题都说要使用ADOX。 Even Microsoft's tutorials involve ADOX. 甚至Microsoft的教程都涉及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. 如果这是最好的方法,我完全愿意添加ADOX并仍然使用OLEDB,或者如果它更快/更高效,甚至可以切换到SQLite,但是我只是问是否有仅OLEDB的方式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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