C# 使用 ADO.NET 创建新数据库 (.mdb)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/346581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:00:35  来源:igfitidea点击:

Creating a new database (.mdb) with ADO.NET

c#databaseado.netms-jet-ace

提问by Malfist

How can I create a empty .mdb file? I'm using ADO.NET and C#. Thanks!

如何创建一个空的 .mdb 文件?我正在使用 ADO.NET 和 C#。谢谢!

采纳答案by MatthewMartin

Copy a pre-existing .mdb file is the best way.

复制预先存在的 .mdb 文件是最好的方法。

The same is true for most of the other filebased database formats that ADO.NET can connect to, such as Excel files. Since a file based database system is using the filesystem as it's host and API for communication with the outside world (as opposed to say MSSQL which communicates using TCP-IP), it quite natural to use System.IO for actions that in say MS-SQL would be done with T-SQL or system stored procedures or a data specific API that targets thoses (say SMO in SQL server's case).

对于 ADO.NET 可以连接到的大多数其他基于文件的数据库格式(例如 Excel 文件),情况也是如此。由于基于文件的数据库系统使用文件系统作为主机和 API 与外部世界进行通信(而不是说 MSSQL 使用 TCP-IP 进行通信),因此使用 System.IO 进行操作是很自然的,例如 MS- SQL 将使用 T-SQL 或系统存储过程或针对这些过程的数据特定 API 来完成(例如 SQL 服务器中的 SMO)。

COPY model.mdb newdb.mdbis the create DB command

COPY model.mdb newdb.mdb是创建数据库命令

DEL newdb.mdb is the dropDB command, etc.

DEL newdb.mdb is the dropDB命令等

回答by Tomalak

I don't think there is a ".NET native" way to do it, but you still can wrap ADOX:

我不认为有一种“.NET 原生”的方式来做到这一点,但你仍然可以包装 ADOX:

using ADOX;  // add a COM reference to "Microsoft ADO Ext. x.x for DDL and Security" 

static void CreateMdb(string fileNameWithPath)
{
  ADOX.Catalog cat = new ADOX.Catalog();
  string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=5";
  cat.Create(String.Format(connstr, fileNameWithPath));
  cat = null;
}