The level of outsourcing is really not flattering, as long as the effect never takes into account efficiency
Look at this code:
<summary>////Add a data///</summary>//<param name= "model" > Return request entity </ param> public int Addreturnorder (www.bpj.com.webservice.Model.ReturnOrder Model, int retractabledays) { Get order ID StringBuilder strSQL = new StringBuilder (); Strsql.append ("select"); Strsql.append ("OrderId"); Strsql.append (", DateDiff (Day, Createtime, GETDATE ()) as days"); Strsql.append ("from"); Strsql.append ("Orders"); Strsql.append ("where"); Strsql.append ("ORDERSN = @OrderSn"); Strsql.append ("and IsDeleted = 0"); Strsql.append ("and userdeleted = 0"); sqlparameter[] Parameters = {New SqlParameter ("@OrderSn", SqlDbType.NVarChar, 50)}; Parameters[0]. Value = model. ORDERSN; DataSet Dsorder = Sqlserverhelper.query (strsql.tostring (), parameters); DataTable Dtorder = Dsorder.Tables[0]; String Strorderid = String. Empty; if (DtOrder.Rows.Count = = 0) {return-1; } else {int days = Int. Parse (dtorder.rows[0]["days"). ToString ()); if (days >= retractabledays) {return-2; } Strorderid = dtorder.rows[0]["OrderId"]. ToString (); }//Get a refundable quantity strSQL = new StringBuilder (); Strsql.append ("select"); Strsql.append ("Orderproductsid, Retractablecount"); Strsql.append ("from"); Strsql.append ("orderproducts"); Strsql.append ("where"); Strsql.append ("OrderId = @OrderId"); Strsql.append ("and productskuid = @ProductSKUId"); Strsql.append ("and IsDeleted = 0"); Parameters = new sqlparameter[] {new SqlParameter ("@OrderId", SqlDbType.Int, 4), New SqLparameter ("@ProductSKUId", SqlDbType.Int, 4)}; Parameters[0]. Value = Strorderid; PARAMETERS[1]. Value = model. Productskuid; DataSet dsorderproduct = Sqlserverhelper.query (strsql.tostring (), parameters); DataTable dtorderproduct = dsorderproduct.tables[0]; String strorderproductsid = String. Empty; if (DtOrderProduct.Rows.Count = = 0) {return-1; } else {int retractablecount = Int. Parse (dtorderproduct.rows[0]["Retractablecount"). ToString ()); if (Retractablecount-model. Returncount < 0) {return-3; } Strorderproductsid = dtorderproduct.rows[0]["Orderproductsid"]. ToString (); }//Order Commodity table Update dictionary<string, sqlparameter[]> dicsqlstringlist = new DICTIONARY<STRING,SQ Lparameter[]> (); strSQL = new StringBuilder (); Strsql.append ("update orderproducts set retractablecount = retractablecount-@ReturnCount from orderproducts whe Re orderproductsid = @OrderProductsId "); Parameters = new sqlparameter[] {new SqlParameter ("@ReturnCount", SqlDbType.Int, 4), New SqlParameter ("@O Rderproductsid ", SqlDbType.Int, 4)}; Parameters[0]. Value = model. Returncount; PARAMETERS[1]. Value = Strorderproductsid; Dicsqlstringlist.add (Strsql.tostring (), parameters); Sqlserverhelper.executesql (Strsql.tostring (), parameters); strSQL = new StringBuilder (); Strsql.append ("INSERT INTO Returnorder ("); Strsql.append ("Isdeleted,userdeleted,orderid,productskuid,userid,returncount,applyreturnmoney,returnmoney, Createtime,status,returnorderreason,returndealresult,returnordersn,paypic) "); Strsql.append ("values"); Strsql.append ("@IsDeleted, @UserDeleted, @OrderId, @ProductSKUId, @UserId, @ReturnCount, @APplyreturnmoney,0,getdate (), 1, @ReturnOrderReason, NULL, @ReturnOrderSn, NULL) "); Strsql.append ("; Select @[email protected] @IDENTITY"); Parameters = new sqlparameter[] {new SqlParameter ("@IsDeleted", sqldbtype.bit,1), New SqlParameter ("@UserDeleted", sqldbtype.int,4), New SqlParameter ("@OrderId", sqldbtype.int,4), New SqlParameter ("@ProductSKUId", sqldbtype.int,4), New SqlParameter ("@UserId", sqldbtype.int,4), New SqlParameter ("@ReturnCount", sqldbtype.int,4), New SqlParameter ("@ Applyreturnmoney ", sqldbtype.decimal,9), New SqlParameter (" @ReturnOrderReason ", Sqldbtype.ntext), new Sq Lparameter ("@ReturnOrderSn", sqldbtype.nvarchar,50)}; Parameters[0]. Value = model. IsDeleted; PARAMETERS[1]. Value = model. userdeleted; PARAMETERS[2]. Value = Strorderid; PARAMETERS[3]. Value = model. Productskuid; PARAMETERS[4]. Value = model. UserId; PARAMETERS[5]. Value = model. Returncount; parameters[6]. Value = model. Applyreturnmoney; PARAMETERS[7]. Value = model. Returnorderreason; PARAMETERS[8]. Value = model. RETURNORDERSN; Dicsqlstringlist.add (Strsql.tostring (), parameters); Sqlserverhelper.executesqltran (dicsqlstringlist); Return Request ID strSQL = new StringBuilder (); Strsql.append ("Select Returnorderid from returnorder where returnordersn = @ReturnOrderSn"); Parameters = new sqlparameter[] {new SqlParameter ("@ReturnOrderSn", SqlDbType.NVarChar, 50)}; Parameters[0]. Value = model. RETURNORDERSN; DataSet Dsret = Sqlserverhelper.query (strsql.tostring (), parameters); int intId =-1; if (Dsret.tables[0]. Rows.Count > 0) {intId = Int. Parse (Dsret.tables[0]. rows[0]["Returnorderid"]. ToString ()); } return intId; }
This new method connects the database 5 times the Great God! You are bdqn graduated from the bar!!!
Brother or optimize it:
public int Add (API. Model.mreturnorder model) {int rowsaffected; sqlparameter[] Parameters = {New SqlParameter ("@ReturnOrderId", sqldbtype.int,4), New SqlParameter ("@ProductSKUId", sqldbtype.int,4), New SqlParameter ("@UserId", sqldbtype.int,4), New SqlParameter ("@ReturnCount", sqldbtype.int,4), New SqlParameter ("@ApplyReturnMoney", sqldbtype.decimal,9), New SqlParameter ("@ReturnOrderReason", Sqldbtype.ntext) , New SqlParameter ("@ReturnOrderSn", sqldbtype.nvarchar,50), New SqlParameter ("@Orde RSn ", sqldbtype.nvarchar,50)}; Parameters[0]. Direction = ParameterDirection.Output; PARAMETERS[1]. Value = model. Productskuid; PARAMETERS[2]. Value = model. UserId; PARAMETERS[3]. Value = model. Returncount; PARAMETERS[4]. Value = model. Applyreturnmoney; PARAMETERS[5]. Value = model. Returnorderreason; PARAMETERS[6]. Value = DateTime.Now.ToString ("yyyymmddhhmmssfff") + moDel. Userid.padleft (5, ' 0 ') + "RO"; Dbhelpersql.runprocedure ("Api_returnorder_add", parameters, out rowsaffected); return (int) parameters[0]. Value; }
Stored procedures:
CREATE PROCEDURE api_returnorder_add@returnorderid int output, @ProductSKUId int, @UserId int, @ReturnCount int,@ Applyreturnmoney Decimal (18,2), @ReturnOrderReason ntext, @ReturnOrderSn nvarchar (@OrderSn nvarchar) as declare @OrderId int DECLARE @d int DECLARE @OrderProductsId int DECLARE @RetractableCount int if exists (select Orde RSn from Orders where ordersn = @OrderSn and IsDeleted = 0 and userdeleted = 0) begin select @OrderId =orderid, @d =datediff (Day, Createtime, GETDATE ()) from Orders where ordersn = @OrderSn and IsDeleted = 0 and userdeleted = 0 if @d>=7 begin select @OrderProductsId =orderproductsid, @RetractableCount =retractablecount from Orderproduct s where OrderId = @OrderId and Productskuid = @ProductSKUId and IsDeleted = 0 if @ @ROWCOUNT >0 begin if (@[email protected]) <0 begin set @ReturnOrderId =-3 End Begin Update OrDerproducts Set retractablecount = retractablecount-@ReturnCount from orderproducts where orderproductsid = @OrderProduc TsId INSERT into [Returnorder] ([isdeleted],[userdeleted],[orderid],[productskuid],[userid],[returncount],[a pplyreturnmoney],[returnmoney],[createtime],[status],[returnorderreason],[returnordersn],[ Rebatecalculatedstatus] VALUES (0,0, @OrderId, @ProductSKUId, @UserId, @ReturnCount, @ApplyReturnMoney, 0,getdate (), 1 , @ReturnOrderReason, @ReturnOrderSn, 0) SET @ReturnOrderId = @ @IDENTITY end end Else b Egin set @ReturnOrderId =-1 end END Begin Set @ReturnOrderId =-2 end end Else begin set @ReturnOrderId =-1 Endgo
This article is purely spit bad, do not spray!
True love life, away from outsourcing! Ha ha