[英]Excel VBA connect to MySql server
I set up a test database at db4free.net and uploaded a copy of the northwind training database to it, to see if I could pull some information to an excel workbook and keep getting the generic unspecified/automation error.我在 db4free.net 上建立了一个测试数据库,并将 Northwind 培训数据库的副本上传到它,看看我是否可以将一些信息提取到 excel 工作簿并不断收到通用的未指定/自动化错误。
I included "Microsoft ActiveX Data Objects 2.8 library" in the references and even tried 6.1 for good measure.我在参考资料中包含了“Microsoft ActiveX Data Objects 2.8 library”,甚至尝试了 6.1 以获得良好的衡量标准。
Before anyone freaks out at me including the username and password;在任何人吓坏我之前,包括用户名和密码; the only thing that exists on this test database is a training dataset.这个测试数据库上唯一存在的是一个训练数据集。 I have ZERO personal information stored there.我在那里存储了零个人信息。
Here is my code:这是我的代码:
Sub sqlTest()
'Declare some strings to hold the connection string and the SQL statement
Dim cnStr As String
Dim sqlStr As String
'Define a connection and a recordset to hold extracted information
Dim oConn As ADODB.Connection
Dim rcSet As New ADODB.Recordset
Set oConn = New ADODB.Connection
Set rcSet = CreateObject("ADODB.Recordset")
'connection string to connect to db4free.net
cnStr = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=85.10.205.173;DATABASE=resumedemo;PORT=3306;UID=jwaycaster;PWD=resumedemo123;"
'Test SQL query
sqlStr = "SELECT * FROM `Employees`"
'This is where it crashes
oConn.Open cnStr
oConn.CommandTimeout = 900
rcSet.Open sqlStr, oConn
Sheets(1).Range("A1").CopyFromRecordset rcSet
rcSet.Close
oConn.Close
End Sub
I've search around several related topics and can't seem to find the answer.我搜索了几个相关主题,但似乎找不到答案。 Hopefully I'm missing something simple.希望我错过了一些简单的东西。
It sometimes can be useful to handle the errors yourself.有时自己处理错误会很有用。 Add references to添加引用到
Option Explicit
Sub sqlTest()
' credentials
Const SERVER = "85.10.205.173"
Const DB = "resumedemo"
Const UID = "jwaycaster"
Const PWD = "resumedemo123"
'Define a connection and a recordset to hold extracted information
Dim oConn As ADODB.Connection, rcSet As ADODB.Recordset
Dim cnStr As String, n As Long, msg As String, e
'connection string to connect to db4free.net
cnStr = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=" & SERVER & _
";PORT=3306;DATABASE=" & DB & _
";UID=" & UID & ";PWD=" & PWD & ";"
'Test SQL query
Const SQL = "SELECT * FROM `Employees`"
' connect
Set oConn = New ADODB.Connection
'oConn.CommandTimeout = 900
On Error Resume Next
oConn.Open cnStr
If oConn.Errors.Count > 0 Then
For Each e In oConn.Errors
msg = msg & vbLf & e.Description
Next
MsgBox msg, vbExclamation, "ERROR - Connection Failed"
Exit Sub
Else
MsgBox "Connected to database " & oConn.DefaultDatabase, vbInformation, "Success"
End If
' run query
Set rcSet = oConn.Execute(SQL, n)
If oConn.Errors.Count > 0 Then
msg = ""
For Each e In oConn.Errors
msg = msg & vbLf & e.Description
Next
MsgBox msg, vbExclamation, "ERROR - Execute Failed"
Else
Sheets(1).Range("A1").CopyFromRecordset rcSet
MsgBox SQL & " returned " & n & " records", vbInformation
End If
On Error GoTo 0
oConn.Close
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.