如何在 C# 中在运行时创建 Access 数据库? [英] How to create an Access database at runtime in C#?

查看:34
本文介绍了如何在 C# 中在运行时创建 Access 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C# 中在运行时创建 Access 数据库?

How can I create an Access Database at runtime in C#?

推荐答案

您需要做的第一件事是获得对 Microsoft ADO Ext 的 COM 引用.X.X 用于 DDL 和安全性.X.X 代表您的机器上碰巧拥有的任何版本.我的曾经是 2.7 版,但是在 Visual Studio 2008 中,它更新到了 6.0.

The first thing you need to do is get a COM reference to the Microsoft ADO Ext. X.X for DDL and Security. The X.X represents whatever version you happen to have on your machine. Mine used to be version 2.7, but with Visual Studio 2008, it was updated to 6.0.

替代文字 http://blog.jrpsoftware.com/content/binary/WindowsLiveWriter/CreateanAccessDatabaseinC_10DDD/AddReference_2.png

添加引用后,ADOX 将添加到代码的 using 部分.

Once you have added the reference, ADOX will be added to the using section of your code.

替代文字 http://blog.jrpsoftware.com/content/binary/WindowsLiveWriter/CreateanAccessDatabaseinC_10DDD/Using_2.png

接下来您需要为数据库创建目录.将您想要的文件名插入以下字符串并将其传递给 CatalogClass.

Next you will want to create the catalog for the database. Insert the filename you wish into the following string and pass it to the CatalogClass.

CatalogClass cat = new CatalogClass();  
string tmpStr;  
string filename = "Sample.MDB";   
tmpStr = "Provider=Microsoft.Jet.OLEDB.4.0;";   
tmpStr += "Data Source=" + filename + ";Jet OLEDB:Engine Type=5";  
cat.Create(tmpStr);

下一步是为您的数据库创建表和列.这是一个非常直接的操作,如下面的示例所示.

The next step is to create the table and columns for your database. This is a pretty straight forward operation as shown in the example below.

 Table nTable = new Table(); 
 nTable.Name = "PersonData"; 
 nTable.Columns.Append("LastName", DataTypeEnum.adVarWChar, 25);
 nTable.Columns.Append("FirstName", DataTypeEnum.adVarWChar, 25);
 nTable.Columns.Append("Address 1", DataTypeEnum.adVarWChar, 45);
 nTable.Columns.Append("Address 2", DataTypeEnum.adVarWChar, 45); 
 nTable.Columns.Append("City", DataTypeEnum.adVarWChar, 25);
 nTable.Columns.Append("State", DataTypeEnum.adVarWChar, 2);
 nTable.Columns.Append("Zip", DataTypeEnum.adVarWChar, 9);
 cat.Tables.Append(nTable);

最后一步非常重要,否则关闭应用程序时会出错.添加完所有表和列后,您需要以正确的顺序正确释放 com 对象.

The final step is very important or you will get error when you close your application. Once the all the tables and columns have been added, you will need to release the com objects properly and in the proper order.

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(nTable); 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.Tables);    
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection); 
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);

就是这样.您现在应该有一个可以写入的 Access 数据库.希望这会有所帮助.

That is it. You should now have a Access Database that you can write to. Hope this helps.

参考:John Russell Plant - 创建访问数据库在 C# 中

这篇关于如何在 C# 中在运行时创建 Access 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆