我用这个函数创建了一个数据库:
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
我希望通过代码连接它,我试过这个,但它不起作用:
Try
Dim constring As String
constring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Utilizador.Utilizador-PC\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\Doc_Vendas_Cab.mdf;"
Dim conn As New SqlConnection(constring)
conn.Open()
MsgBox("Connected")
conn.Close()
Catch Ex As Exception
MsgBox(Ex)
End Try
我应该怎么做?
答案 0 :(得分:0)
您在代码中使用ms-access数据库为什么使用 SqlConnection 提供程序,它是用于SQL Server数据库的。
您必须使用 OleDb.OleDbConnection
提供商。
请参阅此示例链接 helpful
Try
Dim constring As String
constring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Utilizador.Utilizador-PC\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\Doc_Vendas_Cab.mdf;"
Dim conn As New OleDb.OleDbConnection(constring)
conn.Open()
MsgBox("Connected")
conn.Close()
Catch Ex As Exception
MsgBox(Ex)
End Try