mysql - VB.NET 向 MySQL 缓慢插入 Datagrid 行

标签 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

最佳答案

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

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

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

关于mysql - VB.NET 向 MySQL 缓慢插入 Datagrid 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14852745/

相关文章:

asp.net - 使用 asp.net 触发 javascript 事件

vb.net - Win 10 全新安装后,如何为 VS2015 安装 Office 互操作程序集?

asp.net - 站点访问期间未找到网络路径错误

javascript - 使用 Javascript、PHP 和 AJAX 输出 SQL 查询

mysql - 尝试在 MySql 中创建存储过程但出现错误

java - 从mysql数据库解析数据时出错

php - 我怎样才能使这个功能更简单?未知的数组深度

mysql - Codeigniter 模型出现错误 : Column count doesn't match value count at row 2

javascript - CausesValidation 检查所有验证器...甚至验证组?

vb.net - 添加多个文本框.net