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
,我必须在主表中插入摘要,在明细表中插入详细信息。一切正常。我将遵循以下场景:
撤消主表中的最后一次更新(使用query)。
我使用导入System.Data.Odbc
连接到mysql
以下是msdn中关于
End Sub如果您使用的是ado.net
,则可以使用命令类的事务。请参阅更新:am usingImports 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