我如何重置Transactionscope并在之后提交?

时间:2018-02-15 14:45:49

标签: c# transactionscope

我怎样才能做到这一点?这是可能的还是我应该宣布另一个交易范围?

    using (TransactionScope scope = new TransactionScope())
        {
            using (SqlConnection connection1 = new SqlConnection(connectString1))
            {
                connection1.Open();

                SqlCommand command1 = new SqlCommand(NomProcedure, connection1);
                command1.CommandType = CommandType.StoredProcedure;
                command1.ExecuteNonQuery();

                //Rollback transaction Here 

                SqlCommand command2 = new SqlCommand("dbo.AddHistory", connection1);
                command2.CommandType = CommandType.StoredProcedure;
                command2.ExecuteNonQuery();

            }

            scope.Complete();
        }

我只想提交第二个命令

1 个答案:

答案 0 :(得分:2)

从您开始交易的那一刻起,在您完成交易之前,不会确认任何数据更改。

如果您想要回滚,那么就不要致电String userInput; do{ System.out.print("Enter your first name: "); String firstName2 = input.nextLine(); System.out.print("Enter your last name: "); String lastName2 = input.nextLine(); System.out.print("Enter your address: "); String address2 = input.nextLine(); System.out.print("Enter your city: "); String city2 = input.nextLine(); System.out.print("Enter your state: "); String state2 = input.nextLine(); System.out.print("Enter your zip code + 4: "); String zip2 = input.nextLine(); System.out.print("Enter amount owed: "); String amount2 = input.nextLine(); System.out.print("Enter your payment amount: "); String payment2 = input.nextLine(); System.out.print("Enter the date of payment: "); String date2 = input.nextLine(); System.out.println("\t\t\t\t\t" + "XYZ Hospital"); System.out.println(); System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment"); System.out.println(); System.out.println("Last" + "\t" + "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date"); System.out.println(); System.out.println(lastName2 + " " + firstName2 + "\t" + address2 + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2 + "\t\t" + payment2 + "\t\t" + date2); System.out.print("Would you like to enter another patient? Type Yes or No: "); userInput = input.nextLine(); }while (userInput.equals("Yes"));

Complete()

这可以通过在using (TransactionScope scope = new TransactionScope()) { ... // do not call this to rollback what you did before scope.Complete(); } 内进行交易(如msdn建议)或有条件地调用try/catchComplete)时抛出异常来完成。

如果你想在内部回滚事务中执行命令,那么它应该是这样的:

if