批量插入范围ADODB Excel Vba

时间:2018-07-09 14:23:44

标签: excel vba excel-vba

我有一个电子表格UI,用户可以在其中输入数据。然后,我需要收集数据并使用ADODB连接将其插入SQL Server。

当前,我的工作解决方案是从i = 7(数据开始处)循环到i = LastRow,并在每个循环中执行INSERT操作,然后将发送的行标记为“ Y”。

这很好,但是我担心这可能会导致延迟或冗余,特别是如果用户输入的数据超过1000x行。换句话说,这将创建1000多个INSERTS ...

我的代码是:

Pre-loop code
            For i = 7 To LastRow

            'Check if it was sent - then skip iteration
                    With Worksheets("Admin")
                        If .Cells(i, 5).Value = "Y" Then
                            GoTo NextIteration
                        Else
                            Command.CommandText = "INSERT INTO [dbo].[TEP_Payments_Table] ([AA Number], [AA Name], [AA Role], [Project Name], [Series], [Paper No], [Task Name], [Amount], [Payment Justification], [Payment Identifier], [Date of Activity], [Half Day / Full Day], [Teacher Release (Y/N)], [Centre No], [Request Receipt Id], [Request Date], [Requested By], [Business Unit]) VALUES (" & _
"'" & Sheets("Project_Name").Cells(i, 2).Value & "'," & _
"'" & Replace(Replace(Replace(Sheets("Project_Name").Cells(i, 3).Value, "'", ""), "*", ""), Chr(34), "") & "'," & _
"'" & Replace(Replace(Replace(Sheets("Project_Name").Cells(i, 4).Value, "'", ""), "*", ""), Chr(34), "") & "'," & _
"'" & Sheets("Project_Name").Cells(i, 5).Value & "'," & _
"'" & Sheets("Project_Name").Cells(i, 6).Value & "'," & _
"'" & Sheets("Project_Name").Cells(i, 7).Value & "'," & _
"'" & Sheets("Project_Name").Cells(i, 8).Value & "'," & _
"'" & Sheets("Project_Name").Cells(i, 9).Value & "'," & _
"'" & "Description: " & Replace(Replace(Replace(Sheets("Project_Name").Cells(i, 10).Value, "'", ""), "*", ""), Chr(34), "") & vbNewLine & vbNewLine & _
"  //Project: " & Sheets("Project_Name").Cells(i, 5).Value & vbNewLine & _
"  //Series: " & Sheets("Project_Name").Cells(i, 6).Value & vbNewLine & _
"  //Paper No: " & Sheets("Project_Name").Cells(i, 7).Value & vbNewLine & _
" //Task Name: " & Sheets("Project_Name").Cells(i, 8).Value & "'," & _
"'" & Replace(Replace(Replace(Sheets("Project_Name").Cells(i, 12).Value, "'", ""), "*", ""), Chr(34), "") & "'," & _
"'" & Replace(Replace(Replace(Sheets("Project_Name").Cells(i, 13).Value, "'", ""), "*", ""), Chr(34), "") & "'," & _
"'" & Replace(Replace(Replace(Sheets("Project_Name").Cells(i, 14).Value, "'", ""), "*", ""), Chr(34), "") & "'," & _
"'" & Replace(Replace(Replace(Sheets("Project_Name").Cells(i, 15).Value, "'", ""), "*", ""), Chr(34), "") & "'," & _
"'" & Replace(Replace(Replace(Sheets("Project_Name").Cells(i, 16).Value, "'", ""), "*", ""), Chr(34), "") & "'," & _
"'" & .Cells(i, 22).Value & "'," & _
"'" & Format(Now(), "yyyy-MM-dd hh:mm:ss") & "'," & _
"'" & Application.UserName & "'," & _
"'" & .Cells(i, 20).Value & "')"


                                'replace(replace(replace(Sheets("Project_Name").Cells(i, 10).Value,"'",""),"*",""),Chr(34),"")

                            Command.Execute

                            'Mark what was inserted with "Y"
                                .Cells(i, 5).Value = "Y"
                                Sheets("Project_Name").Cells(i, 19).Value = 

"Y"
'''''' After loop code ''''

我想知道是否有比循环遍历每一行更快的方法?

0 个答案:

没有答案