Introduction to TransactionScope in Tran and C # in SQL

Source: Internet
Author: User
Tags getdate try catch

Tran two ways of writing in SQL

The first form of a try catch:

BeginTRYBegin Tran Transaction            DECLARE @ServiceOrderId INT         SET @ServiceOrderId=73342        DECLARE @ErrorCount  INT         SET @ErrorCount = 0        --Adjust the point-to- order status payment complete        UPDATEdbo. ServiceordernewSETOrderstatus= 2, Updatedate= GETDATE()        WHEREServiceorderid= @ServiceOrderId        --Adjust Order Details        UPDATEdbo. ServiceordernewdetailSETDetailstatus= 2        WHEREServiceorderid= @ServiceOrderId     Commit Tran Transaction    ENDTRYBEGINCATCHROLLBACK Tran TransactionENDCatch

The second notation accumulates with errors:

DECLARE @ServiceOrderId INT SET @ServiceOrderId=73342 BEGIN TRANADECLARE @ErrorCount  INT         SET @ErrorCount = 0        --Adjust the point-to- order status payment complete        UPDATEdbo. ServiceordernewSETOrderstatus= 2, Updatedate= GETDATE()             WHEREServiceorderid= @ServiceOrderId                SET @ErrorCount=@ErrorCount+@ @error        --Adjust Order Details        UPDATEdbo. ServiceordernewdetailSETDetailstatus= 2        WHEREServiceorderid= @ServiceOrderId                       SET @ErrorCount=@ErrorCount+@ @error        --Payment Status        UPDATEdbo. ScorepayapplySETApplystatus= 2        WHEREScorepayapplyid=(SELECT TOP 1Scorepayapplyid fromServiceordernewdetailWHEREServiceorderid= @ServiceOrderId)                 SET @ErrorCount=@ErrorCount+@ @error                SET @ErrorCount=@ErrorCount+@ @error           IF @ErrorCount=0       BEGIN           COMMIT TRANAEND   ELSE         BEGIN           ROLLBACK TRANAEND 

Second TransactionScope usage

Tran = new TransactionScope ())        {            try            {                serviceitemdataaccess.servicesconfirmpayment (Serviceorderid, remark, serviceId);                 // commit a                transaction Tran . Complete ();            }            catch (Exception ex)            {                return  ex. ToString ();            }            Finally            {                tran. Dispose ();            }        }

TransactionScope is a lightweight thing, in the using, if the program error, then automatically rollback. Tran.complete (); means the program succeeds, commits. Tran. Dispose (); Release and destruction.

Introduction to TransactionScope in Tran and C # in SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.