我尽力使用 VB.NET 为 Access (.accdb) 数据库添加 ODBC 系统数据源。从谷歌搜索我尝试了很多功能,但没有任何效果。
我试过的代码是:
Sub createDSN()
Const ODBC_ADD_SYS_DSN = 4 ' Add data source
Dim dbpath As String = "C:\Jenit\Data\001.accdb"
Dim ret As Integer, Driver As String, Attributes As String
Driver = "Microsoft Access Driver (*.MDB,*.accdb)" & Chr(0)
Attributes = "DSN=" & "Hello" & Chr(0)
Attributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=pwd" & Chr(0)
Attributes = Attributes & "DBQ=" & dbpath & Chr(0) & Chr(0)
ret = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, Driver, Attributes) 'Error Here
'ret is equal to 1 on success and 0 if there is an error
If ret <> 1 Then
MsgBox("DSN Creation Failed")
Else
MsgBox("Successful")
End If
End Sub 'Main
错误是:
调用 PInvoke 函数 'j!j.Form1::SQLConfigDataSource' 使堆栈失衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。
请帮忙。