取消上次在数据库中进行的更新
本文关键字:更新 数据库 取消 | 更新日期: 2023-09-27 18:35:40
我有一个主表和一个通过引用master_gid
与之关联的详细表,我必须在主表中插入摘要并在详细表中插入详细信息。 一切正常。 我遵循以下场景:
- 插入主表
- 如果主表插入成功,则插入详细表
- 如果详细表插入失败,则从主表中删除引用字段
这段时间一切都很好。
在更新的情况下,我遵循相同的方案,但在详细表插入失败的情况下面临问题。如何undo
(使用查询)主表中的最后一次更新detail_table以防插入失败。正在使用Imports System.Data.Odbc
连接到mysql
下面是
msdn 中关于使用事务的示例
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
结束子