vb.net 如何以编程方式生成新的 MS 访问文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16082071/
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
How to generate a new MS access file programmatically
提问by Gutanoth
I have looked far and wide, in the deepest darkest corners of the internet, but for the life of me, I can not find the correct way to open a NEW Access file and then using vb.net to write data in the database..
我已经在互联网的最深处最黑暗的角落里四处张望,但对于我的生活,我找不到打开新访问文件然后使用 vb.net 在数据库中写入数据的正确方法..
The keywords here are NEW database, I don't want to open an existing file.
这里的关键字是新数据库,我不想打开现有文件。
Is this even possible?
这甚至可能吗?
Thanks in advance!
提前致谢!
回答by Gutanoth
I have finally found the way, thanks to a co-worker of mine
多亏了我的一个同事,我终于找到了方法
Neither ADO.NET nor ActiveX Data Object (ADO) provides the means to create Microsoft Access Database. However, we can create Access databases by using the Microsoft Jet OLE DB Provider and Microsoft ADO Ext. 2.7 for DDL and Security (ADOX) with the COM Interop layer. To do so, select References from the Project Menu, choose the COM tab, and add a reference to Microsoft ADO Ext. 2.7 for DDL and Security; then you can use this function.
ADO.NET 和 ActiveX 数据对象 (ADO) 都没有提供创建 Microsoft Access 数据库的方法。但是,我们可以使用 Microsoft Jet OLE DB Provider 和 Microsoft ADO Ext 创建 Access 数据库。2.7 用于具有 COM 互操作层的 DDL 和安全性 (ADOX)。为此,请从“项目”菜单中选择“引用”,选择“COM”选项卡,然后添加对 Microsoft ADO Ext 的引用。2.7 对于 DDL 和安全性;那么你就可以使用这个功能了。
When you have done this, use the following snippet to create a database
完成此操作后,使用以下代码段创建数据库
Public Class Form1
Private Sub btnLoad_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnLoad.Click
CreateAccessDatabase("C:\test\testDB.mdb")
MsgBox("Database created")
End Sub
Public Function CreateAccessDatabase( ByVal DatabaseFullPath As String) As Boolean
Dim bAns As Boolean
Dim cat As New ADOX.Catalog()
Try
Dim sCreateString As String
sCreateString =_
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
DatabaseFullPath
cat.Create(sCreateString)
bAns = True
Catch Excep As System.Runtime.InteropServices.COMException
bAns = False
Finally
cat = Nothing
End Try
Return bAns
End Function
End Class

