2

Visual Basic 2010 Express
MySQL Connector/Net 5.2.7
MySQL Server 5.5.16
Windows 7 Professional 64Bit
还尝试使用 MySQL Connector/Net 5.0.x 在 Windows XP 32 Bit

MySqlDataAdapter 在调用 MySqlCommandBuilder GetUpdateCommand() 方法后在 Select 语句上返回零行。没有错误被抛出,并且更新成功。如果我注释掉 GetUpdateCommand() 和 Update() 行,Select 语句将返回行。如果我只是注释掉 Update() 行,问题仍然存在,所以问题与 GetUpdateCommand() 而不是 Update() 本身有关。

我要连接的数据库中有两个表,每个表都具有相同的架构和内容:

test_table & different_table
id INT 主键
名 VARCHAR(45)

每个有五行:
1 - 名称
2 - 名称
...等

Imports System.Data
Imports MySql.Data.MySqlClient
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Open Connection to test database'
    Dim mscConnection As MySqlConnection
    mscConnection = New MySqlConnection()
    mscConnection.ConnectionString = "server=localhost; user id=root; password=; database=testdb"
    mscConnection.Open()

    'Get records from test table'
    Dim msdaDataAdapter As MySqlDataAdapter
    Dim dsDataSet As New DataSet
    msdaDataAdapter = New MySqlDataAdapter("SELECT id, name FROM test_table", mscConnection)
    msdaDataAdapter.Fill(dsDataSet, "test_table")

    'Show record count (result is 5)'
    Console.WriteLine(dsDataSet.Tables("test_table").Rows.Count)

    'Loop through rows and change the name field of each one'
    For Each row As DataRow In dsDataSet.Tables("test_table").Rows
        row("name") = "new name"
    Next

    'Update altered rows'
    Dim mscbCommandBuilder As New MySqlCommandBuilder()
    mscbCommandBuilder = New MySqlCommandBuilder(msdaDataAdapter)
    mscbCommandBuilder.GetUpdateCommand()
    msdaDataAdapter.Update(dsDataSet, "test_table")

    'Have tried with and without RefreshSchema/Dispose/Nothing'
    mscbCommandBuilder.RefreshSchema()
    mscbCommandBuilder.Dispose()
    mscbCommandBuilder = Nothing

    'Try to Select again'
    'Get records from test table'
    msdaDataAdapter = New MySqlDataAdapter("SELECT id, name FROM test_table", mscConnection)
    msdaDataAdapter.Fill(dsDataSet, "test_table_2")

    'Show record count: result is 0 :/'
    Console.WriteLine(dsDataSet.Tables("test_table_2").Rows.Count)

    'Try a different table'
    msdaDataAdapter = New MySqlDataAdapter("SELECT id, name FROM different_test_table", mscConnection)
    msdaDataAdapter.Fill(dsDataSet, "different_test_table")

    'Show record count: result is 0'
    Console.WriteLine(dsDataSet.Tables("different_test_table").Rows.Count)
    mscConnection.Close()
    mscConnection.Dispose()
End Sub
End Class

如果我再次单击附加到代码的按钮(不关闭/重新打开应用程序),所有Select 语句将返回零行。

Update() 后连接没有关闭。如果我在更新后尝试(重新)打开连接,我会收到一条错误消息,指出连接已打开。

有谁知道发生了什么?如何更新以便我可以继续在代码中进一步选择?

4

0 回答 0