From vb.net how can I rollback transaction which I created deferent Sql Server Stored Procedure

  • HI

    For my application I am using SQL Server2000 and VB.NET,

    I use two SQL Server Stored Procedure to create Sales Order, I have two tables for sales order (1 orderheader , 2 orderdetail)

    First I create record in orderheader table with Stored Procedure (usp_CreateOrderHeader)

    After that I create record in orderdetail table for each items one by one with Stored Procedure (usp_CreateOrderDetail)

    My question

    How can I roll back all record created in Orderheader table and orderdetail table, if system fail in middle while I create Sales Order transaction from VB..net application

    From vb.net how can I rollback transaction which I created deferent Stored Procedure

    Thanks

  • Personally, I prefer to pass all the data to the stored procedure at once..instead of calling a stored procedure for each record. This will give better performance as well as you can handle the transaction at the database side.

    This topic is discussed in detail here:

    http://www.sqlservercentral.com/articles/Miscellaneous/2908/

    http://www.sqlservercentral.com/articles/Miscellaneous/2909/

    http://www.sqlservercentral.com/articles/Miscellaneous/2911/

    http://www.sqlservercentral.com/articles/Stored+Procedures/2912/

    If you still want to execute stored procedures one by one, you can use the transaction processiong features of ado.net connection. Call connection.beginTransaction() and all the commands that you execute after that will be part of a single transaction. Just google for "ado.net transaction" and there are plenty of resources available.

    for example:

    http://www.c-sharpcorner.com/UploadFile/shrijeetnair/TransactionsNConcurr09052005002744AM/TransactionsNConcurr.aspx

    .

  • You can try to use System.Data.SqlClient.SqlTransaction object and execute all sp into the same transaction context.

    If you catch an error you can rollback all change into the transaction context.

  • Mujeeb, go to VB.Net help and search on Ado.net Transactions and you will find a good explanation about SQL Server transactions. This link may work.

    ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEV.v10.en/dnadonet/html/transact.htm

    I would not rewrite your procs as others have suggested, just use the SQLConnection object like this:

    connection.BeginTransaction()

    ' perform your inserts here

    connection.Commit()

    Be sure to use Try Catch blocks and test your rollback logic also.

    Good Luck


    Doug

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply