我故意尝试将我的数据集更改为具有重复的主键,期望此操作会引发mysql错误。
如何从MySQLDataAdapter.Update()
??
我已经简要介绍了事件处理程序,但我似乎无法找到在哪里得到错误。虽然我注意到MySqlRowUpdatingEventArgs.Status = SkipCurrentRow
代码段:
Private Sub UpdateTable()
Dim MyCommandBuilder As New MySqlCommandBuilder(MyDataadapter)
txtConsole.AppendText(vbNewLine & MyDataSet.HasErrors.ToString)
AddHandler MyDataadapter.RowUpdating, AddressOf OnRowUpdating
AddHandler MyDataadapter.RowUpdated, AddressOf OnRowUpdated
If MyDataSet.GetChanges() Is Nothing Then
MessageBox.Show("The table contains no changes to save.")
Else
' Without the MySqlCommandBuilder this line would fail.
Try
Dim rowsAffected As Integer = MyDataadapter.Update(MyDataTable)
If rowsAffected = 0 Then
MessageBox.Show("No rows were affected by the save operation.")
Else
MessageBox.Show(rowsAffected & " rows were affected by the save operation.")
End If
Catch ex As MySqlException
txtConsole.AppendText(vbNewLine & "Error: " & ex.ToString())
End Try
End If
RemoveHandler MyDataadapter.RowUpdating, AddressOf OnRowUpdating
RemoveHandler MyDataadapter.RowUpdated, AddressOf OnRowUpdated
End Sub
' handler for RowUpdating event
Private Shared Sub OnRowUpdating(sender As Object, e As MySqlRowUpdatingEventArgs)
PrintEventArgs(e)
End Sub
' handler for RowUpdated event
Private Shared Sub OnRowUpdated(sender As Object, e As MySqlRowUpdatedEventArgs)
PrintEventArgs(e)
End Sub
Private Overloads Shared Sub PrintEventArgs(args As MySqlRowUpdatingEventArgs)
Console.WriteLine("OnRowUpdating")
Console.WriteLine(" event args: (" & " command=" & args.Command.CommandText & _
" commandType=" & args.StatementType & " status=" & args.Status & ")")
End Sub
Private Overloads Shared Sub PrintEventArgs(args As MySqlRowUpdatedEventArgs)
Console.WriteLine("OnRowUpdated")
Console.WriteLine(" event args: (" & " command=" & args.Command.CommandText & _
" commandType=" & args.StatementType & " recordsAffected=" & _
args.RecordsAffected & " status=" & args.Status & ")")
End Sub