取消数据库中的上次更新

时间:2014-12-31 04:44:57

标签: c# mysql asp.net vb.net winforms

通过引用master_gid,我有一个主表和一个与之关联的详细表,我必须在主表中插入摘要,并在详细表中插入详细信息。一切正常。我遵循这个场景:

  1. 插入主表
  2. 如果主表插入成功,则插入详细表
  3. 如果详细的表插入失败了主要的删除引用字段
  4. 这一切的每件事都很好。

    更新 的情况下,我遵循相同的方案,但在详细的表插入失败的情况下遇到问题。如果detail_table插入失败,我如何undo使用查询)主表中的最后一次更新。 我正在使用Imports System.Data.Odbc来连接mysql

1 个答案:

答案 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