加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

OdbcTransaction.Rollback 方法的VB.NET例子

发布时间:2020-12-16 23:27:51 所属栏目:大数据 来源:网络整理
导读:OdbcTransaction.Rollback 方法的VB.NET例子 下面的示例创建一个 OdbcConnection 和一个 OdbcTransaction 。此示例还演示如何使用 BeginTransaction 、 Commit 和 Rollback 等方法。 Public Sub ExecuteTransaction(ByVal connectionString As String) Using

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读