Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 取消上次在数据库中进行的更新_C#_Mysql_Asp.net_Vb.net_Winforms - Fatal编程技术网

C# 取消上次在数据库中进行的更新

C# 取消上次在数据库中进行的更新,c#,mysql,asp.net,vb.net,winforms,C#,Mysql,Asp.net,Vb.net,Winforms,我有一个主表和一个与之关联的明细表,通过引用主表_gid,我必须在主表中插入摘要,在明细表中插入详细信息。一切正常。我将遵循以下场景: 插入主表 如果主表插入成功,则插入详细表 如果详细表插入失败,则从主表中删除引用字段 这次一切都很顺利 在更新的情况下i遵循相同的场景,但在详细表插入失败的情况下面临问题。如果细节表插入失败,我如何撤消主表中的最后一次更新(使用query)。 我使用导入System.Data.Odbc连接到mysql以下是msdn中关于 End Sub如果您使用的是ado.ne

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

  • 插入主表
  • 如果主表插入成功,则插入详细表
  • 如果详细表插入失败,则从主表中删除引用字段
  • 这次一切都很顺利

    更新的情况下i遵循相同的场景,但在详细表插入失败的情况下面临问题。如果细节表插入失败,我如何
    撤消主表中的最后一次更新(使用query)。
    
    我使用
    导入System.Data.Odbc
    连接到
    mysql

    以下是msdn中关于


    End Sub

    如果您使用的是
    ado.net
    ,则可以使用
    命令类的
    事务
    。请参阅更新:am using
    Imports System.Data.Odbc
    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