vba读取数据库
1、安装数据库
2、创建数据源
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Row = 1And Target.Cells.Column = 1Then
Call mySQL
End If
End Sub
Public Function mySQL()
strconnt = ""
Set connt = CreateObject("ADODB.Connection")
Dim sevip, Db, user, pwd As String
'设服务器地址所连数据,及登录⽤户密码
sevip = "localhost"
Db = "excelTest"
user = "root"
pwd = "900921"
strconnt = "DRIVER={MySql ODBC 5.3 Unicode Driver};SERVER=" & sevip & ";Database=" & Db & ";Uid=" & user & ";Pwd=" & pwd & ";Stmt=set names GBK"
connt.ConnectionString = strconnt
connt.Open
MsgBox"链接状态:" & connt.State & vbCrLf & "ADO版本:" & connt.Version, vbInformation, ""
Set Rec = CreateObject("ADODB.Recordset")
mysql下载odbc失败Set Rec = connt.Execute("select * from `uses`", iRowscount, adCmdText)
Range("a6:c6").Value = Array("id", "name", "password")
Range("a7").CopyFromRecordset Rec
End Function
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论