将数据从datatable插入MySql表

时间:2017-04-13 08:21:12

标签: mysql vb.net

我的代码将数据从datatable插入到Mysql表中,但是过程需要很长时间。无论如何,我可以修改我的方法来制作" One shoot insert"我的数据值?在交易中也很好。感谢。

 Dim result As Boolean = False
        Dim MysqlConn As MySqlConnection
        MysqlConn = New MySqlConnection()
        MysqlConn.ConnectionString = "Server=localhost;Database=duo;Uid=root;Pwd=pass1;"
        Using con As New MySqlConnection(MysqlConn.ConnectionString)

            For Each r As DataRow In _dtSir.Rows

                Using cmd As New MySqlCommand("INSERT INTO " & _serializer.GetTableName(_reportId) & " (SR, IDcol, System) 
                                                VALUES (@SR, @IDcol, @System)", con)

                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@SR", r("SR#"))
                    cmd.Parameters.AddWithValue("@IDcol", CInt(r("ID")))
                    cmd.Parameters.AddWithValue("@System", r("System"))

                    If Not con.State = ConnectionState.Open Then
                        con.Open()
                    End If

                    cmd.ExecuteNonQuery()
                    result = True
                End Using
            Next

        End Using

        Return result
    End Function

1 个答案:

答案 0 :(得分:0)

试试看是否更快:

Public Function test()
    Dim result As Boolean = False
    Using con As New MySqlConnection("Server=localhost;Database=duo;Uid=root;Pwd=pass1;")
        con.Open()
        For Each row As DataRow In_dtSir.Rows
        Using cmd = con.CreateCommand()
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "INSERT INTO " & _serializer.GetTableName(_reportId) & " (SR, IDcol, System) VALUES ('" & row("SR#") & "', '" & CInt(row("ID")) & "','" & row("System") & "'", con)
                cmd.Connection = con
                cmd.ExecuteNonQuery()
                result = True
            End Using
        Next
        con.Close()
        SqlConnection.ClearPool(con)
    End Using
    Return result
End Function

由于我从未使用 MySqlConnection

,因此可能需要调整一些内容。