C # Implementation database transaction case

Source: Internet
Author: User

I am the actual project to take out the case, the scenario is such as when we do the e-commerce site, when the order is created in a series of operations, usually when the order is created we need the operation of the data table: the Order of the table header (main table), order details, empty shopping cart, or even modify the status of the coupon The main method of executing a transaction is directly posted below:

 Public Static BOOLCreateuserorder (Userorder model,stringIds) {List<string> sqlstringlist =Newlist<string>(); List<DbParameter[]> Parameters =NewList<dbparameter[]>(); StringBuilder strSQL=NewStringBuilder (); //Add Orderinforsqlparameter[] parameters1 = Orderinfordal.buildorderinforaddparameter (model. UserId, model. Ordercode, Ids,refstrSQL);            Sqlstringlist.add (Strsql.tostring ());            Parameters.Add (PARAMETERS1); //Add Userordersqlparameter[] Parameters2 = Builduserorderaddparameter (model,refstrSQL);            Sqlstringlist.add (Strsql.tostring ());            Parameters.Add (PARAMETERS2); //Empty Shopping Cartsqlparameter[] Parameters3 = Basketdetaildal.buildupdateparameter (trueIdsrefstrSQL);            Sqlstringlist.add (Strsql.tostring ());            Parameters.Add (PARAMETERS3); //Modify Coupon Status            if(model. Couponid >0) {sqlparameter[] parameters4= Coupondal.buildupdateparameter (model. Couponid,1, model. Ordercode,refstrSQL);                Sqlstringlist.add (Strsql.tostring ());            Parameters.Add (PARAMETERS4); }            returnAccess. Executesqltran (Sqlstringlist, Parameters,true); }

Next, two detailed methods for reference are posted:

Internal StaticSqlparameter[] Buildorderinforaddparameter (intUseridstringOrdercode,stringIdsrefStringBuilder strSQL) {strSQL=NewStringBuilder (); Strsql.append ("INSERT into Orderinfor ("); Strsql.append ("userid,x_proid,x_proname,stocknum,buynum,unitprice,ordercode,totalprice,isother)"); Strsql.append ("Select Userid,x_proid,x_proname,stocknum,buynum,unitprice, @OrderCode, totalprice,isother from Basketdetail "); Strsql.appendformat ("where [email protected] and issubmit= ' false ' and isinquiry=2 and Isexpired=0 and Basketdetailid in ({0})", Ids); sqlparameter[] Parameters= {                    NewSqlParameter ("@OrderCode", SqlDbType.NVarChar, -),                    NewSqlParameter ("@UserId", SqlDbType.Int)}; parameters[0]. Value =Ordercode; parameters[1]. Value =UserId; returnparameters; }
Internal StaticSqlparameter[] Buildupdateparameter (intCouponid,intState,stringOrdercode,refStringBuilder strSQL) {strSQL=NewStringBuilder (); Strsql.append ("Update Coupon Set"); Strsql.append ("[state][email protected],[email protected]"); Strsql.appendformat ("where id={0}", Couponid); sqlparameter[] Parameters= {                    NewSqlParameter ("@State", SqlDbType.Int),NewSqlParameter ("@OrderCode", SqlDbType.NVarChar, -)            }; parameters[0]. Value =State ; parameters[1]. Value =Ordercode; returnparameters; }

C # Implementation database transaction case

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.