private void btnOk_Click(object sender, EventArgs e) { //新增或者修改 if (CheckEmpty()) { ProductInfoBll proBll = new ProductInfoBll(); ProductInfo pro = new ProductInfo(); pro.CId = Convert.ToInt32(cmbCategory.SelectedValue);//获取选择的类别 pro.ProCost = Convert.ToDouble(txtCost.Text);//价格 pro.ProName = txtName.Text;//名字 pro.ProNum = txtNum.Text;//编号 pro.ProPrice = Convert.ToDouble(txtPrice.Text);//实际的价格 pro.ProSpell = txtSpell.Text;//拼音 pro.ProStock = Convert.ToInt32(txtStock.Text); pro.ProUnit = txtUnit.Text;//单位 pro.Remark = txtRemark.Text; ;//备注 if (Temp == 1)//新增 { pro.DelFlag = 0; pro.SubBy = 1; pro.SubTime = System.DateTime.Now;//当前时间 } else if (Temp == 2)//修改 { pro.ProId = Convert.ToInt32(labId.Text); } md.MsgDivShow(proBll.AddOrUpdateProductInfo(pro, Temp) ? "操作成功" : "操作失败", 1, Bind); } }
public static FrmProductAddOrModify Single(int temp, ProductInfo pro) { if (_instance == null || _instance.IsDisposed) { _instance = new FrmProductAddOrModify(pro); Temp = temp;//标识 } return _instance; }
public FrmProductAddOrModify(ProductInfo pro) { InitializeComponent(); LoadCategoryInfoByDelFlag(0);//下拉框绑定数据 //判断对象不能为空 if (pro != null)//修改 { txtCost.Text = pro.ProCost.ToString();//进价 txtName.Text = pro.ProName;//名字 txtNum.Text = pro.ProNum;//编号 txtPrice.Text = pro.ProPrice.ToString();//价格 txtRemark.Text = pro.Remark;//备注 txtSpell.Text = pro.ProSpell;//拼音 txtStock.Text = pro.ProStock.ToString();//库存 txtUnit.Text = pro.ProUnit;//单位 labId.Text = pro.ProId.ToString();//产品信息的id cmbCategory.SelectedValue = pro.CId;//商品类别---显示 } }
private void ShowFrmProductAddOrModify(int temp, ProductInfo pro) { this.Hide();//当前窗体隐藏 FrmProductAddOrModify fpam = FrmProductAddOrModify.Single(temp, pro); fpam.FormClosed += new FormClosedEventHandler(fcam_FormClosed); fpam.Show(); }
//新增 public int AddProductInfo(ProductInfo pro) { string sql = "insert into ProductInfo( CId, ProName, ProCost, ProSpell, ProPrice, ProUnit, Remark, DelFlag, SubTime, ProStock, ProNum, SubBy)values( @CId, @ProName, @ProCost, @ProSpell, @ProPrice, @ProUnit, @Remark, @DelFlag, @SubTime, @ProStock, @ProNum, @SubBy)"; return AddOrUpdateProductInfo(pro, sql, 1);//新增 }
//关系转对象 private ProductInfo RowToProductInfoByDataRow(DataRow dr) { ProductInfo pro = new ProductInfo(); pro.CId = Convert.ToInt32(dr["CId"]); pro.DelFlag = Convert.ToInt32(dr["DelFlag"]); pro.ProCost = Convert.ToDouble(dr["ProCost"]); pro.ProId = Convert.ToInt32(dr["ProId"]); pro.ProName = dr["ProName"].ToString(); pro.ProNum = dr["ProNum"].ToString(); pro.ProPrice = Convert.ToDouble(dr["ProPrice"]); pro.ProSpell = dr["ProSpell"].ToString(); pro.ProStock = Convert.ToInt32(dr["ProStock"]); pro.ProUnit = dr["ProUnit"].ToString(); pro.Remark = dr["Remark"].ToString(); pro.SubBy = Convert.ToInt32(dr["SubBy"]); pro.SubTime = Convert.ToDateTime(dr["SubTime"]); return pro; }
//temp 1---新增,2---修改 private int AddOrUpdateProductInfo(ProductInfo pro, string sql, int temp) { List<SqlParameter> list = new List<SqlParameter>(); SqlParameter[] ps = { new SqlParameter("@CId",pro.CId), new SqlParameter("@ProName",pro.ProName), new SqlParameter("@ProCost",pro.ProCost), new SqlParameter("@ProSpell",pro.ProSpell), new SqlParameter("@Remark",pro.Remark), new SqlParameter("@ProStock",pro.ProStock), new SqlParameter("@ProNum",pro.ProNum), new SqlParameter("@ProPrice",pro.ProPrice), new SqlParameter("@ProUnit",pro.ProUnit) }; list.AddRange(ps);//参数存放到集合中 if (temp == 1) { list.Add(new SqlParameter("@DelFlag", pro.DelFlag)); list.Add(new SqlParameter("@SubTime", pro.SubTime)); list.Add(new SqlParameter("@SubBy", pro.SubBy)); } else if (temp == 2) { list.Add(new SqlParameter("@ProId", pro.ProId)); } return SqlHelper.ExecuteNonQuery(sql, list.ToArray()); }
//修改 public int UpdateProductInfo(ProductInfo pro) { string sql = "update ProductInfo set CId=@CId, ProName=@ProName, ProCost=@ProCost, ProSpell=@ProSpell, ProPrice=@ProPrice, ProUnit=@ProUnit, Remark=@Remark, ProStock=@ProStock, ProNum=@ProNum where ProId=@ProId"; return AddOrUpdateProductInfo(pro, sql, 2); }