首页 > 解决方案 > 为什么连接字符串只能在开发机器上工作?

问题描述

我已经编写了 2 个程序,一个在 VB6 中,一个在 VBA for Excel 中。调用 mySQL 连接只能在开发机器上完美运行。尝试在任何其他机器上运行任一程序都会失败。代码如下:

Private Sub cmdErrors_Click()
On Error GoTo remote_err

Set myCon = New ADODB.Connection
strConnect = "Driver={MySQL ODBC 5.3 ANSI 
Driver};Server=xxx.xxx.xxx.xxx;Port=3306;Database=cl22-budget;User=username; 
Password=password;Option=3;"

myCon.ConnectionString = strConnect
myCon.Open
MsgBox "Connected"
myCon.Close
Set myCon = Nothing
Exit Sub

remote_err:
Dim ErrorCollection As Variant
Dim ErrLoop As Error
Dim strError As String
Dim iCounter As Integer

On Error Resume Next

iCounter = 1
strError = ""
Set ErrorCollection = myCon.Errors
For Each ErrLoop In ErrorCollection
With ErrLoop
   strError = "error # " & iCounter & vbCrLf
   strError = strError & "ADO Error # " & .Number & vbCrLf
   strError = strError & " Description " & .Description & vbCrLf
   strError = strError & " Source " & .Source & vbCrLf
   MsgBox strError
   iCounter = iCounter + 1
End With
Next
End Sub

标签: mysqlexcelvbaodbc

解决方案


安装 ODBC 驱动程序并为您的数据库创建一个 DSN,它会在您提到您的服务器 ip 时询问服务器地址,然后它将起作用。


推荐阅读