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