OdbcTransaction.Rollback 方法的VB.NET例子

来源:互联网 发布:基站 知乎 编辑:程序博客网 时间:2024/05/17 06:27

OdbcTransaction.Rollback 方法的VB.NET例子

 

下面的示例创建一个 OdbcConnection 和一个 OdbcTransaction 。此示例还演示如何使用 BeginTransactionCommitRollback 等方法。

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 UsingEnd Sub
原创粉丝点击