现在,我正在遍历 Excel 电子表格中的每一行,并使用插入查询将每一行插入到我的 Access 表中。它有效,但速度很慢。如何进行一次查询以一次添加所有记录?
这是我当前的代码
Sub Insert()
Dim rs As ADODB.Recordset, strSQL As String, minPrem As Double, i As Long, datetime As Date
datetime = Now()
Call DB.ConnectToDB 'Connect to the database
Set rs = New ADODB.Recordset
i = 7 'first row of data
Do While (Cells(i, 1) <> "")
If (IsNumeric(Cells(i, 6))) Then
minPrem = Cells(i, 6)
Else
minPrem = 0
End If
strSQL = "INSERT INTO Rates (EffectiveDate, State, Company, ClassCode, Rate, MinPremium, TimeOfEntry) VALUES " _
& "(#" & Cells(i, 1) & "#,'" & Cells(i, 2) & "','" & Cells(i, 3) & "','" & Cells(i, 4).Text & "'," & Cells(i, 5) & "," & minPrem & ", #" & datetime & "#)"
rs.Open strSQL, Cn
i = i + 1
Loop
End Sub
最佳答案
包装多个 INSERT
Transaction 中的操作通常可以加快速度,因为单个 INSERT 被缓存,然后一次全部写入(提交)到数据库。尝试添加 cn.BeginTrans
进入循环之前的语句,然后执行 cn.CommitTrans
退出循环后,看看是否有帮助。
此外,如果您只是在执行 INSERT 操作,那么您不需要弄乱单独的 Recordset
目的;你可以做cn.Execute strSQL
.
关于sql - 如何将多行excel数据传输到 Access 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15953349/