【问题标题】:VB.NET Slow Insert of Datagrid rows to MySQLVB.NET 将 Datagrid 行慢速插入到 MySQL
【发布时间】:2013-01-28 22:50:07
【问题描述】:

我有一个例程将 CSV 导入数据网格,然后只需一个 for-next 循环来插入每一行,但它的运行速度很慢,需要 18 分钟才能插入 7100 行。

现在我已经将连接调用从循环中取出,所以它只执行一次,执行插入,然后在完成时关闭。

我已经看到提到一次批量插入 100 行可能会有所帮助,但这可能需要一些复杂的例程来计算其当前最多的行,然后在这么多 100 行之后计算奇数。

有没有一种简单的方法,只是说插入整个数据网格而不是 100 行而没有 for/next 循环?

正如您将在下面看到的那样,为了方便阅读,我已经分解了我的查询字符串,将它合并成一行会有很大的不同吗?在您回复时,我可能已经尝试过了:

        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

【问题讨论】:

    标签: mysql vb.net


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      这是因为您正在建立一个新连接并为每一行创建一个新的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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-17
        • 1970-01-01
        相关资源
        最近更新 更多