繁体   English   中英

如何使用 VB.net 在 sql server 中执行提交/回滚

[英]How to do Commit/rollback in sql server using VB.net

我正在开发一个涉及 sql server 作为数据库的 asp.net 应用程序。 在 vb.net 中编写了巨大的函数后,我不得不选择、插入和更新不同的表。 我意识到,如果所有这些都一次性执行,那么这是一个双赢的局面。 如果这一切都不顺利,那么就会造成巨大的混乱。

当我们在 Oracle 中进行 DML 操作时,我们必须

commit;

rollback;

在每次 DML 操作之后。 我的问题是我们如何使用 VB.net 在 sql server 中做同样的事情。

我的搜索导致编写一个过程@sql server。 插入和删除将通过排序过程完成。 但我希望它像正常操作一样

SqlCommand("some query", connection")

是否可以在不使用排序过程的情况下进行提交或回滚?

提前致谢!

您还可以使用TransactionScope ,它提供了比自己管理事务更简洁的代码。

Using transaction As NewTransactionScope()

    Using connection As New SqlConnection(connectionString)
        connection.Open()

        Dim command As SqlCommand = connection.CreateCommand()

        command.CommandText = _
          "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"

        command.ExecuteNonQuery()

        command.CommandText = _
          "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"

        command.ExecuteNonQuery()

    End Using

    ' If we do not run Commit(), e.g. an error occurs before we get here,
    ' the transaction will automatically roll back when we leave the Using block below
    transaction.Commit()

End Using

您应该使用SqlTransaction 这是来自 MSDN 的无耻复制粘贴:

Private Sub ExecuteSqlTransaction(ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        connection.Open()

        Dim command As SqlCommand = connection.CreateCommand()
        Dim transaction As SqlTransaction

        ' Start a local transaction
        transaction = connection.BeginTransaction("SampleTransaction")

        ' Must assign both transaction object and connection 
        ' to Command object for a pending local transaction.
        command.Connection = connection
        command.Transaction = transaction

        Try
            command.CommandText = _
            "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
            command.ExecuteNonQuery()
            command.CommandText = _
            "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"

            command.ExecuteNonQuery()

            ' Attempt to commit the transaction.
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")

        Catch ex As Exception
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType())
            Console.WriteLine("  Message: {0}", ex.Message)

            ' Attempt to roll back the transaction. 
            Try
                transaction.Rollback()
            Catch ex2 As Exception
                ' This catch block will handle any errors that may have occurred 
                ' on the server that would cause the rollback to fail, such as 
                ' a closed connection.
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType())
                Console.WriteLine("  Message: {0}", ex2.Message)
            End Try 
        End Try 
    End Using 
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM