0
在vb腳本中我是全新的。 我試着去與當地的MI MySQL數據庫與此代碼連接:與vb腳本的mysql連接(來自excel宏)
Option Explicit
Private CN As ADODB.Connection
Function Connect(Server As String, Database As String) As Boolean
Set CN = New ADODB.Connection
On Error Resume Next
With CN
.ConnectionString = "Driver={MySQL ODBC 5.3 Driver};Server=" & _
Server & ";Database=" & Database & _
";Uid=user;Pwd=password;"
.Open
End With
If CN.State = 0 Then
Connect = False
Else
Connect = True
End If
End Function
Function Query(SQL As String)
Dim RS As ADODB.Recordset
Dim Field As ADODB.Field
Dim Col As Long
Set RS = New ADODB.Recordset
RS.Open SQL, CN, adOpenStatic, adLockReadOnly, adCmdText
If RS.State Then
Col = 1
For Each Field In RS.Fields
Cells(1, Col) = Field.Name
Inc Col
Next Field
Cells(2, 1).CopyFromRecordset RS
Set RS = Nothing
End If
End Function
Function Disconnect()
CN.Close
End Function
Private Sub SQL_Click()
Dim SQL As String
Dim Connected As Boolean
SQL = "Select * from table1"
Connected = Connect("localhost", "table")
If Connected Then
Call Query(SQL)
Call Disconnect
Else
MsgBox "Could Not Connect!"
End If
End Sub
總是顯示不能瞬移。 ADODB庫是好的,我的數據庫參數可以在從mysql手動導入時使用,並且驅動程序版本是正確的。我不知道發生了什麼...
有些幫助嗎?
謝謝!