4

我有一个例程将 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
4

2 回答 2

0

无需将整个 csv 加载到数据网格中,您可以一次仅加载一个完整的屏幕(或多一点),然后在页面上重新加载页面上的其他记录,页面向下或搜索。您还可以使用虚拟模式:

http://msdn.microsoft.com/en-us/library/ms171621.aspx

如果你有一个巨大的查询来一次加载它,你可能想要使用 stringBuilder 而不是字符串。这可以防止另一个瓶颈。

于 2013-02-13T17:30:42.503 回答
0

这是因为您正在建立一个新的连接并为每一行创建一个新的MySqlCommand和一个新的对象。MySqlConnection由于一一输入许多数据,这应该会很慢。尝试更改您的代码以执行一堆 sql 命令,如下所示:

    Query = ""
    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 &= "');"
    Next

    con = New MySqlConnection("Data Source=" & vardbpath & ";Database=" & vardbname & ";User ID=" & txtUsername.Text & ";Password=" & txtPassword.Text & ";")
    con.Open()
    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
于 2016-01-31T15:02:26.987 回答