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