通过引用master_gid
,我有一个主表和一个与之关联的详细表,我必须在主表中插入摘要,并在详细表中插入详细信息。一切正常。我遵循这个场景:
这一切的每件事都很好。
在更新 的情况下,我遵循相同的方案,但在详细的表插入失败的情况下遇到问题。如果detail_table插入失败,我如何undo
(使用查询)主表中的最后一次更新。
我正在使用Imports System.Data.Odbc
来连接mysql
答案 0 :(得分:0)
以下是msdn关于using transaction
的示例Public Sub ExecuteTransaction(ByVal connectionString As String)
Using connection As New OdbcConnection(connectionString)
Dim command As New OdbcCommand()
Dim transaction As OdbcTransaction
' Set the Connection to the new OdbcConnection.
command.Connection = connection
' Open the connection and execute the transaction.
Try
connection.Open()
' Start a local transaction.
transaction = connection.BeginTransaction()
' Assign transaction object for a pending local transaction.
command.Connection = connection
command.Transaction = transaction
' Execute the commands.
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
command.ExecuteNonQuery()
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
command.ExecuteNonQuery()
' Commit the transaction.
transaction.Commit()
Console.WriteLine("Both records are written to database.")
Catch ex As Exception
Console.WriteLine(ex.Message)
' Try to rollback the transaction
Try
transaction.Rollback()
Catch
' Do nothing here; transaction is not active.
End Try
End Try
' The connection is automatically closed when the
' code exits the Using block.
End Using
End Sub