OdbcTransaction.Rollback 方法的VB.NET例子

前端之家收集整理的这篇文章主要介绍了OdbcTransaction.Rollback 方法的VB.NET例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 Using
End Sub
@H_502_11@ 原文链接:https://www.f2er.com/vb/262925.html

猜你在找的VB相关文章