我有一个例程将 CSV 导入数据网格,然后只需一个 for-next 循环来插入每一行,但它的运行速度很慢,需要 18 分钟才能插入 7100 行。
现在我已经将连接调用从循环中取出,所以它只执行一次,执行插入,然后在完成时关闭。
我已经看到提到一次批量插入 100 行可能会有所帮助,但这可能需要一些复杂的例程来计算其当前最多的行,然后在这么多 100 行之后计算奇数。
有没有一种简单的方法可以说插入整个数据网格而不是没有 for/next 循环的 100 行?
正如您将在下面看到的那样,为了方便阅读,我已经分解了我的查询字符串,将它连接成一行会有很大的不同吗?在您回复时,我可能已经尝试过了:
con = New MySqlConnection("Data Source=" & vardbpath & ";Database=" & vardbname & ";User ID=" & txtUsername.Text & ";Password=" & txtPassword.Text & ";")
con.Open()
For i = 0 To rows - 1
Query = "INSERT into GENERAL (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad)"
Query &= "values ('"
Query &= (DataGrid1.Rows(i).Cells(0).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(1).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(2).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(3).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(4).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(5).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(6).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(7).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(8).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(9).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(10).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(11).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(12).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(13).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(14).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(15).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(16).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(17).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(18).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(19).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(20).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(21).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(22).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(23).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(24).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(25).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(26).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(27).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(28).Value)
Query &= "','" + (DataGrid1.Rows(i).Cells(29).Value)
Query &= "')"
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
Try
Dim j As Integer = cmd.ExecuteNonQuery()
If (j > 0) Then txt_folderactivity.Text &= "Row Successfully Inserted" & vbCrLf
Catch myerror As MySqlException
txt_folderactivity.Text &= "Error Executing SQL command: " & myerror.Message & vbCrLf
End Try