. Net and SqlServer transaction processing instances

Source: Internet
Author: User

1. A general error handling mode for SQL Server Stored Procedure transaction processing is roughly as follows: Create procdure prInsertProducts (@ intProductId int, @ chvProductName varchar (30), @ intProductCount int) ASDeclare @ intErrorCode intSelect @ intErrorCode = @ ErrorBegin transaction if @ intErrorCode = 0 begin-insert products (ProductID, ProductName, ProductCount) values (@ intProductId, @ chvProductName, @ intProductCount) select @ intErrorCode = @ Error -- check every execution of a t-SQL statement immediately, save the Error code to the local variable. end if @ intErrorCode = 0 begin-update products set ProductName = 'microcomputer' where ProductID = 5 Select @ intErrorCode = @ Error endif @ intErrorCode = 0 commit transactionelse rollback transaction Return @ intErrorCode -- it is best to Return the error code to the called stored procedure or application 2 ,. use transactions to process SqlConnection myConnection = new SqlConnection ("Data Source = localhost; Initial Catalog = Northwind; Integrated Security = SSPI;"); myConnection. open (); SqlTransaction myTrans = myConnection. beginTransaction (); // use New to generate a new transaction SqlCommand myCommand = New SqlCommand (); myCommand. transaction = myTrans; try {myCommand. commandText = "Update Address set location = '23 rain street 'where userid = '000000'"; myCommand. executeNonQuery (); myTrans. commit (); Console. writeLine ("Record is udated. ");} catch (Exception e) {myTrans. rollback (); Console. writeLine (e. toString (); Console. writeLine ("Sorry, Record can not be updated. ");} finally {myConnection. close ();}

Related Article

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.