1、定义连接db的过程:Dim oConn As ADODB.ConnectionPrivate 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 Sub2、执行db插入的具体写法:Dim rs As ADODB.RecordsetPrivate 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 WithEnd Sub
Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有