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

时间:2014-09-21 20:18:56

标签: asp.net sql-server vb.net

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

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

commit;

rollback;
每次DML操作后

。我的问题是我们如何使用VB.net在sql server中做同样的事情。

我的搜索导致编写一个过程@sql server。通过分类程序进行灭菌和通货紧缩。但我希望它像正常的操作,如

SqlCommand("some query", connection")

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

提前致谢!

2 个答案:

答案 0 :(得分:4)

您还可以使用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

答案 1 :(得分:3)

您应该使用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