如何将excel中的数据用ADO插入数据库中的数据表中?

来源:百度知道 编辑:UC知道 时间:2024/06/15 02:17:57
问题1:
请问在哪条语句中体现insert into value……
问题2:
请问执行完insert语句后,在哪里写commit?

给你一个例子,你参考一下就可以做出来了:
Sub 支出登记_Click()
Dim i As Integer, j As Integer, k As Integer, sht As Worksheet 'i,j,k为整数变量;sht 为excel工作表对象变量,指向某一工作表
Dim cn As New ADODB.Connection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
Dim rs As New ADODB.Recordset '定义记录集对象,保存数据表
Dim strCn As String, strSQL As String '字符串变量
Dim mdbFile As String

On Error GoTo add_err

mdbFile = ActiveWorkbook.Path & "\生活数据库.mdb"

strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbFile '定义数据库链接字符串
cn.Open strCn '与数据库建立连接,如果成功,返回连接对象cn

Set rs = New ADODB.Recordset
rs.Open "dbl支出登记表", cn, adOpenKeyset, adLockOptimistic
rs.AddNew
rs(1) = Cells(5, 3)
rs(2) = Cells(6, 3)
rs(3) = Cells(7, 3)
rs(4) = Cells(8, 3)
rs(5) = Cells(9, 4)
rs(6) = Cells(10, 4)
rs.Update
Cells(5, 3) = Date
Cells(6, 3) = ""