我有一个既有After Insert触发器又有After Update触发器的表。如果我在一个命令中同时插入记录和更新记录,那么触发器触发的顺序是什么?
发布于 2015-06-22 22:22:54
在构建了一个测试项目之后,我能够得到答案。触发器按照行在表中的顺序触发。因此,如果我有一个带有After Insert、After Update和After Delete触发器的表,它们将按照触发它们的行的顺序触发。这在MySQL 5.6中进行了测试。
这是您要的样品
Public Function UpdateTriggerrecords(ByVal dt As DataTable) As Boolean
Try
Return PerformUpdate(dt, SETUPTriggerrecords_UPDATE(), SETUPTriggerrecords_INSERT(), SETUPTriggerrecords_DELETE(), False)
Catch ex As Exception
Throw ex
End Try
End Function
Private Function SETUPTriggerrecords_UPDATE() As MySqlCommand
Dim cmd As New MySqlCommand("UPDATE tbltriggerrecords SET strDescription=@strDescription, intTestingValue=@intTestingValue " & _
"WHERE idTriggerRecords=@idTriggerRecords")
CreateTriggerrecordsParameters(cmd)
CreateTriggerrecordsIDParameters(cmd)
Return cmd
End Function
Private Function SETUPTriggerrecords_INSERT() As MySqlCommand
Dim cmd As New MySqlCommand("INSERT INTO tbltriggerrecords(strDescription,intTestingValue) " & _
"VALUES(@strDescription,@intTestingValue)")
CreateTriggerrecordsParameters(cmd)
Return cmd
End Function
Private Function SETUPTriggerrecords_DELETE() As MySqlCommand
Dim cmd As New MySqlCommand("DELETE FROM tbltriggerrecords WHERE idTriggerRecords=@idTriggerRecords")
CreateTriggerrecordsIDParameters(cmd)
Return cmd
End Function
Private Sub CreateTriggerrecordsIDParameters(ByVal cmd As MySqlCommand)
cmd.Parameters.Add("@idTriggerRecords", MySqlDbType.Int32).SourceColumn = "idTriggerRecords"
End Sub
Private Sub CreateTriggerrecordsParameters(ByVal cmd As MySqlCommand)
cmd.Parameters.Add("@strDescription", MySqlDbType.VarChar).SourceColumn = "strDescription"
cmd.Parameters.Add("@intTestingValue", MySqlDbType.Int32).SourceColumn = "intTestingValue"
End Sub
Private Function PerformUpdate(ByVal dt As DataTable, ByVal cmdUpdate As MySqlCommand, ByVal cmdInsert As MySqlCommand, ByVal cmdDelete As MySqlCommand, ByVal capturePrimaryKeyValue As Boolean) As Boolean
If connMySQL Is Nothing Then
connMySQL = New MySqlConnection(connstrMySQL)
End If
Using connMySQL
If connMySQL.State <> ConnectionState.Open Then
connMySQL.ConnectionString = connstrMySQL
connMySQL.Open()
End If
trans = connMySQL.BeginTransaction
Dim da As New MySqlDataAdapter("", connMySQL)
cmdUpdate.Connection = connMySQL
cmdUpdate.Transaction = trans
cmdInsert.Connection = connMySQL
cmdInsert.Transaction = trans
If cmdDelete IsNot Nothing Then
cmdDelete.Connection = connMySQL
cmdDelete.Transaction = trans
End If
Try
If capturePrimaryKeyValue = True Then
AddHandler da.RowUpdated, New MySqlRowUpdatedEventHandler(AddressOf OnRowUpdatedMySQL)
End If
da.UpdateCommand = cmdUpdate
da.InsertCommand = cmdInsert
If cmdDelete IsNot Nothing Then
da.DeleteCommand = cmdDelete
End If
da.Update(dt)
If capturePrimaryKeyValue = True Then
RemoveHandler da.RowUpdated, AddressOf OnRowUpdatedMySQL
End If
PerformUpdate = True
trans.Commit()
Catch ex As Exception
trans.Rollback()
PerformUpdate = False
Finally
If connMySQL.State <> ConnectionState.Closed Then
connMySQL.Close()
End If
End Try
End Using
End Functionhttps://stackoverflow.com/questions/30925046
复制相似问题