VB.Net MySQL 连接器。批量插入停止工作

VB.Net MySQL Connector. Bulk insert stopped working

几年来,我一直在使用下面的代码在 MySQL 数据库中填充 table,并且运行良好。

Imports MySql.Data.MySqlClient

Public sub Thing(dt as datatable)

            Try
                'dt is datatable object populated from MSsqlServer
                Dim MySQLcmdSelect As MySqlCommand = New MySqlCommand("Select * From " & tableName) With {
                    .Connection = MySQLcnn
                }
                Dim MySQLad As New MySqlDataAdapter(MySQLcmdSelect)
                Dim MySQLcmdBuilder As New MySqlCommandBuilder(MySQLad)
                Dim MySQLcmd As MySqlCommand = MySQLcmdBuilder.GetInsertCommand()
                MySQLcmd.Connection = MySQLcnn
                MySQLad.InsertCommand = MySQLcmd
                MySQLad.Update(dt)
            Catch ex As Exception
                Debug.Print(ex.Message)
                Console.WriteLine("Error when populating " + tableName + ": " + ex.Message + vbCrLf + ex.InnerException.ToString)
                WriteLog(ex.Message)
            End Try
End Sub

问题是,它停止工作了!

连接器似乎工作正常,因为我可以删除 tables 并创建它们,但这不会让我批量插入数据内容table 'dt' MySQL table.

它不会抛出异常,当 dt 中有 1000 行时,它只是通过 .update(dt) 行,就好像它是 Debug.Print() 行一样。

我实际上是从 MS SQL 服务器中的 table 中选择性地提取数据,然后将它们批量上传到 table 中 MySQL 中同名的数据。

a) 这是最好的方法吗,并且 b) 为什么这突然停止工作了?

根据 phpMyAdmin 其服务器版本:5.6.51 - MySQL 社区服务器 (GPL)

编辑:更多信息...所有 SQL 命令删除现有 tables 并创建 tables 工作。只有 .update(dt) 不起作用。

您的代码可能存在的问题可能是架构不完全匹配、自动递增列,或者连接可能已在其他地方处理(它应该在使用它的方法中声明为本地)。另一个问题是 dt 调用了 AcceptChangesLoadOptions 设置不正确。

    Private ConnStr As String = "Your connection string"
    Public Sub Thing(dt As DataTable, tableName As String)
        'check the datatable - if it is ok then this check is probably not necessary
        Dim changesDT = dt.GetChanges()
        If changesDT.Rows.Count < 1 Then
            MessageBox.Show("No changes to update")
            Exit Sub
        End If
        Using cn As New MySqlConnection(ConnStr),
            MySQLad As New MySqlDataAdapter("Select * From " & tableName, cn)
cn.open() ' added by OP
            Dim MySQLcmdBuilder As New MySqlCommandBuilder(MySQLad)
            MySQLad.Update(dt)
        End Using
    End Sub

也许你有“0000-00-00”?如果是这样,则设置更改了默认值 -- MySQL 的新版本不允许零日期。了解 sql_modes

仍然无法使批量上传工作,但我们已经重新设计了它,以便在我们将数据存储在 MS-SQL 数据库的同时将数据插入 MySQL 并且删除了批量上传的需要。