VB.NET慢慢将Datagrid行插入MySQL

时间:2013-02-13 11:41:54

标签: mysql vb.net

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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分: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