Dim oConn As ADODB.Connection
Private Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.1 Driver}" &_
"SERVER=localhost" &_
"DATABASE=yourdatabase" &_
"USER=yourdbusername" &_
"PASSWORD=yourdbpassword" &_
"Option=3"
End Sub
2、执行db插入的具体写法:
Dim rs As ADODB.Recordset
Private Sub InsertData()
Set rs = New ADODB.Recordset
ConnectDB
With wsBooks
For rowCursor = 2 To 11
strSQL = "INSERT INTO tutorial (author, title, price) " &_
"VALUES ('" &esc(.Cells(rowCursor, 1)) &"', " &_
"'" &esc(.Cells(rowCursor, 2)) &"', " &_
esc(.Cells(rowCursor, 3)) &")"
rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
Next
End With
End Sub
我有一个较为简单的办法不用VBA编程
如果你知道SQL语句的写法
可以利用EXCEL中的公式直接生成对应的 SQL插入语句
然后把这个语句复制到文本文件保存,直接在MYSQL中执行就行了
我就是这么做的
写VBA太麻烦
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)