事务使用方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq389216533/article/details/51793429
        public string AddRK(StockStockIN model, List<StockINCHKDetail> list)
        {
            SqlConnection connection = new SqlConnection(wdgj31sqlcon);
            SqlCommand command = connection.CreateCommand();
            SqlTransaction transaction;
            connection.Open();
            transaction = connection.BeginTransaction();
            command.Transaction = transaction;
            string bResult = "";
            try
            {
                //概要
                command.CommandText = "G_CreateStockInBill";
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter[] StockInBill = new SqlParameter[] {
                new SqlParameter("@ProviderID",SqlDbType.Int),
                new SqlParameter("@WareHouseID",SqlDbType.Int),
                new SqlParameter("@PriceSpec",SqlDbType.VarChar,20),
                 new SqlParameter("@PriceDis",SqlDbType.Money),
                 new SqlParameter("@theCause",SqlDbType.VarChar,20),
                 new SqlParameter("@regOperator",SqlDbType.VarChar,20),
                 new SqlParameter("@GoodsTotal",SqlDbType.Money),
                 new SqlParameter("@OtherFee",SqlDbType.Money),
                 new SqlParameter("@FlagID",SqlDbType.Int),
                 new SqlParameter("@Remark",SqlDbType.VarChar,200),
                 new SqlParameter("@LogisticNO",SqlDbType.VarChar,50),
                 new SqlParameter("@LogisticFee",SqlDbType.Money),
                 new SqlParameter("@LogisticID",SqlDbType.Int),
                 new SqlParameter("@OperationID",SqlDbType.Int),
                 new SqlParameter("@OperationType",SqlDbType.Int),
                 new SqlParameter("@bChgCost",SqlDbType.Bit),
                 new SqlParameter("@CurrencyType",SqlDbType.VarChar,20),
                 new SqlParameter("@StockINID",SqlDbType.Int),
                };
                StockInBill[0].Value = model.ProviderID;
                StockInBill[1].Value = model.WareHouseID;
                StockInBill[2].Value = model.PriceSpec;
                StockInBill[3].Value = model.PriceDis;
                StockInBill[4].Value = model.theCause;
                StockInBill[5].Value = model.RegOperator;
                StockInBill[6].Value = model.GoodsTotal;
                StockInBill[7].Value = model.OtherFee;
                StockInBill[8].Value = model.FlagID;
                StockInBill[9].Value = model.Remark;
                StockInBill[10].Value = model.LogisticNO;
                StockInBill[11].Value = model.LogisticFee;
                StockInBill[12].Value = model.LogisticID;
                StockInBill[13].Value = model.OperationID;
                StockInBill[14].Value = model.OperationType;
                StockInBill[15].Value = model.bChgCost;
                StockInBill[16].Value = model.CurrencyType;
                StockInBill[17].Direction = ParameterDirection.Output;
                command.Parameters.AddRange(StockInBill);
                command.ExecuteNonQuery();


                int BillID = Convert.ToInt32(StockInBill[17].Value);


                //详情
                //command.CommandText = "G_WriteStockInDetail";
                //command.CommandType = CommandType.StoredProcedure;


                //SqlParameter[] parms = new SqlParameter[] {
                //new SqlParameter("@RecNO",SqlDbType.Int),
                //new SqlParameter("@BillID",SqlDbType.Int),
                //new SqlParameter("@WareHouseID",SqlDbType.Int),
                // new SqlParameter("@GoodsNO",SqlDbType.VarChar,50),
                // new SqlParameter("@GoodsSpec",SqlDbType.VarChar,50),
                // new SqlParameter("@GoodsUnit",SqlDbType.VarChar,20),
                // new SqlParameter("@GoodsCount",SqlDbType.Money),
                // new SqlParameter("@Price",SqlDbType.Money),
                // new SqlParameter("@OtherCost",SqlDbType.Money),
                // new SqlParameter("@Remark",SqlDbType.VarChar,50)
                //};
                //command.CommandType = CommandType.Text;
                //string sqlText = "";
                //SqlParameter[] p = new SqlParameter[list.Count * 8 + 1];
                //p[0] = new SqlParameter("@BillID1", BillID);
                //int k = 0;
                //int z = 1;
                //for (int i = 0; i < list.Count; i++)
                //{
                //    p[k += 1] = new SqlParameter("@GoodsID" + i, list[i].GoodsID);
                //    p[k += 1] = new SqlParameter("@SpecID" + i, list[i].SpecID);
                //    p[k += 1] = new SqlParameter("@Unit" + i, list[i].Unit);
                //    p[k += 1] = new SqlParameter("@GoodsCount" + i, list[i].GoodsCount);
                //    p[k += 1] = new SqlParameter("@Price" + i, list[i].Price);
                //    p[k += 1] = new SqlParameter("@OtherCost" + i, list[i].OtherCost);
                //    p[k += 1] = new SqlParameter("@Remark" + i, list[i].Remark);
                //    p[k += 1] = new SqlParameter("@PositionsID" + i, list[i].PositionsID);
                //    sqlText += "insert into G_Stock_StockINDetail(RecNO,BillID,GoodsID,SpecID,Unit,GoodsCount,Price,OtherCost,Remark,PositionsID)";
                //    sqlText += "values("+z+",@BillID1,@GoodsID"+i+",@SpecID"+i+ ",@Unit"+i+",@GoodsCount"+i+",@Price"+i+",@OtherCost"+i+",@Remark"+i+",@PositionsID"+i+");";
                //}
                //command.CommandText = sqlText;
                //command.Parameters.AddRange(p);
                //identity = Convert.ToInt32(command.ExecuteNonQuery());
                command.CommandText = "G_WriteStockInDetail";
                command.CommandType = CommandType.StoredProcedure;
                int RecNo = 1;
                foreach (var item in list)


                {
                    //SqlParameter[] spadetail = new SqlParameter[12];
                    SqlParameter[] spadetail = new SqlParameter[] {
                        new SqlParameter("@RecNO",SqlDbType.Int),
                        new SqlParameter("@BillID",SqlDbType.Int),
                        new SqlParameter("@WareHouseID",SqlDbType.Int),
                        new SqlParameter("@GoodsNO",SqlDbType.VarChar,50),
                        new SqlParameter("@GoodsSpec",SqlDbType.VarChar,50),
                        new SqlParameter("@GoodsUnit",SqlDbType.VarChar,20),
                        new SqlParameter("@GoodsCount",SqlDbType.Money),
                        new SqlParameter("@Price",SqlDbType.Money),
                        new SqlParameter("@OtherCost",SqlDbType.Money),
                        new SqlParameter("@Remark",SqlDbType.VarChar,500),
                        new SqlParameter("@Positions",SqlDbType.VarChar,50),
                        new SqlParameter("@bResult",SqlDbType.VarChar,50),
                    };
                    spadetail[0].Value = RecNo;
                    spadetail[1].Value = BillID;
                    spadetail[2].Value = model.WareHouseID;
                    spadetail[3].Value = item.GoodsNo;
                    spadetail[4].Value = item.SpecName;
                    spadetail[5].Value = item.Unit;
                    spadetail[6].Value = item.GoodsCount;
                    spadetail[7].Value = item.Price;
                    spadetail[8].Value = 0;
                    spadetail[9].Value = item.Remark;
                    spadetail[10].Value = item.PositionsName;
                    spadetail[11].Direction = ParameterDirection.Output;
                    RecNo++;
                    command.Parameters.Clear();//需要先清空之前的参数
                    command.Parameters.AddRange(spadetail);
                    command.ExecuteNonQuery();
                    bResult = spadetail[11].Value.ToString();
                }
                command.CommandText = "G_PurchaseOrderIN";
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter[] spa = new SqlParameter[] {
                    new SqlParameter("@PurchaseOrderNO",SqlDbType.VarChar,50),
                    new SqlParameter("@StockINID",SqlDbType.Int),
                    new SqlParameter("@bResult",SqlDbType.VarChar,20)
                };
                spa[0].Value = model.OrderNo;
                spa[1].Value = BillID;
                spa[2].Direction = ParameterDirection.Output;
                command.Parameters.Clear();
                command.Parameters.AddRange(spa);
                command.ExecuteNonQuery();
                bResult = spa[2].Value.ToString();
                if (bResult == "1")
                {
                    command.CommandType = CommandType.Text;
                    string sqlText = "insert into G_purchase_log(OrderId,Operator,LogDetail,LogTime)  values(@OrderId,@Operator,'入库结算',@LogTime);";
                    SqlParameter[] spastr = new SqlParameter[3];
                    spastr[0] = new SqlParameter("@OrderId", model.OperationID);
                    spastr[1] = new SqlParameter("@Operator", model.RegOperator);
                    spastr[2] = new SqlParameter("@LogTime",DateTime.Now);
                    command.CommandText = sqlText;
                    command.Parameters.AddRange(spastr);
                    command.ExecuteNonQuery();
                }
                transaction.Commit();//提交事务
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                connection.Close();
                transaction.Dispose();
                connection.Dispose();
            }


            return bResult;
        }

猜你喜欢

转载自blog.csdn.net/qq389216533/article/details/51793429
今日推荐