Transaction inside the code

I have a problem when I don’t know how to solve this, and I want to know what is the best approach I should consider to achieve this.

We are developing the application VB.net 2.0 and SQL 2005. Users can cancel the reception on the basis of a purchase, which may contain many received goods. But in the process of canceling some questions, users such as “Do you want to cancel“ Good No. 1 ”ask. If yes, delete. Then:“ You want to cancel “Good No. 2”, no, do not delete another question (if received element, the process must be performed manually by the user). And, in the end, if all the goods have been successfully canceled, we must cancel the reception itself. But sometime, if an error occurs or some conditions arise after a request to the user in this process, we want to cancel any actions taken from the very beginning and return them to their original state. So I thought about Transaction.

  • I know that there is Transaction for SQL that can be used, and I know how to use it well, but I can’t really use it, since the user has to perform actions that can cancel this transaction.

  • I also recalled TransactionScope from .NET 2.X and over which something similar could be achieved, and I also know how to use it well. The problem is with TransactionScope and MSDTC. When using this, we still get the error message:

Network Access for Distributed Transaction Manager (MSDTC) is disabled. Enable DTC to access the network in the security configuration for MSDTC using the component services administration tool.

, ... . , , . , , , On. , 10 . - 300 , ​​ , , , .

- , ? - , , ?

1. , , . , , . , let say, # 4? ?

2: , -.

3. # , VB, #.

+3
1

, :

create proc CancelGood (@goodID int)
as
   SET NOCOUNT ON
   SET XACT_ABORT ON

   begin transaction

   update table1 set canceled = 1
    where GoodID = @GoodID

   update table2 set on_stock = on_stock + 1
    where GoodID = @GoodID

   commit transaction

VB canceledGoods, "Oui". VB.Net; # :

canceledGoods.Add (string.Format("exec dbo.CancelGood {0}", goodID));

, , :

batch = "BEGIN TRANSACTION" +
        " BEGIN TRY " +
        string.Join (Environment.NewLine, canceledGoods.ToArray()) + 
        " END TRY" +
        " BEGIN CATCH " +
        " -- CODE TO CALL IF THERE WAS AN ERROR" +
        "    ROLLBACK TRANSACTION" +
        "    RETURN" +
        " END CATCH" +
        " -- CODE TO CALL AFTER SUCCESSFULL CANCELATION OF ALL GOODS" +
        " COMMIT TRANSACTION"

conn.ExecuteNonQuery (batch);
+1

All Articles