我在互联网最深处的最黑暗的角落看了很远,但对于我的生活,我找不到正确的方法来打开一个新的Access文件,然后使用vb.net在数据库中写入数据. 这里的关键字是NEW数据库,我不想打开现有
这里的关键字是NEW数据库,我不想打开现有文件.
这甚至可能吗?
提前致谢!
我终于找到了办法,多亏了我的同事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.
完成此操作后,使用以下代码段创建数据库
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
