protected void btnAdd_Click(object sender, EventArgs e) { ProductClass myProductClass = new ProductClass(); myProductClass.PC_Name = this.txtAcname.Text.Trim(); myProductClass.Parent_Id = Convert.ToInt32(this.DropDownListTree.SelectedValue); if (ProductClassManage.Insert(myProductClass)) { ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加产品分类成功!!!')", true); LoadTree(); } else { this.lblError.Visible = true; } }
///<summary>条件查询产品分类</summary> ///<param name="pc_id">产品分类Id</param> ///<returns>ProductClass模型 包含一条产品分类记录.</returns> public static ProductClass GetItem(int pc_id) { ProductClass myProductClass = new ProductClass(); string sql = "SELECT * FROM t_productclass WHERE PC_Id=?PC_Id"; MySqlParameter[] parms = { new MySqlParameter("?PC_Id", MySqlDbType.Int32)}; parms[0].Value = pc_id; MySqlDataReader reader = DbHelper.ExecuteDataReader(sql, parms); if (reader.HasRows) { if (reader.Read()) { myProductClass = FillDataRecord(reader); } } reader.Close(); return myProductClass; }
/// <summary> /// 初始化一个ProductClass类实体,并填充数据。 /// </summary> private static ProductClass FillDataRecord(IDataRecord myDataRecord) { ProductClass myProductClass = new ProductClass(); myProductClass.PC_Id = myDataRecord.GetInt32(myDataRecord.GetOrdinal("PC_Id")); myProductClass.PC_Name = myDataRecord.GetString(myDataRecord.GetOrdinal("PC_Name")); if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Parent_Id"))) { myProductClass.Parent_Id = myDataRecord.GetInt32(myDataRecord.GetOrdinal("Parent_Id")); } myProductClass.PC_Order = myDataRecord.GetInt32(myDataRecord.GetOrdinal("PC_Order")); return myProductClass; }
/// <summary> /// 修改产品分类记录 /// </summary> /// <param name="myProductClass">myProductClass 模型</param> /// <returns>返回<c>true</c>修改成功,或<c>false</c>修改失败.</returns> public static bool Update(ProductClass myProductClass) { int result = 0; string sql = "update t_productclass set pc_name = ?pc_name where pc_id = ?pc_id"; MySqlParameter[] parms = { new MySqlParameter("?pc_name",MySqlDbType.VarChar), new MySqlParameter("?pc_id",MySqlDbType.Int32) }; parms[0].Value = myProductClass.PC_Name; parms[1].Value = myProductClass.PC_Id; result = DbHelper.ExecuteNonQuery(sql, parms); return result > 0; }
/// <summary> /// 插入产品分类记录 /// </summary> /// <param name="myProductClass">myProductClass 模型</param> /// <returns>返回<c>true</c>插入成功,或<c>false</c>插入失败.</returns> public static bool Insert(ProductClass myProductClass) { int result = 0; int? parent_id = null; int pc_order; string sql1,sql2; if (myProductClass.Parent_Id > 0) { parent_id = myProductClass.Parent_Id; } MySqlParameter[] parms = null; if (parent_id == null) { sql1 = "select count(*) from t_productclass where parent_id is null"; } else { sql1 = "select count(*) from t_productclass where parent_id = ?Parent_id"; parms = new MySqlParameter[] { new MySqlParameter("?Parent_id",MySqlDbType.Int32), }; parms[0].Value = parent_id; } pc_order = Convert.ToInt32(DbHelper.ExecuteScalar(sql1, parms)); MySqlParameter[] params2 = null; if (parent_id == null) { sql2 = "insert into t_productclass(pc_name,pc_order) values (?pc_name,?pc_order)"; params2 = new MySqlParameter[]{ new MySqlParameter("?pc_name",MySqlDbType.VarChar), new MySqlParameter("?pc_order",MySqlDbType.Int32) }; params2[0].Value = myProductClass.PC_Name; params2[1].Value = pc_order + 1; } else { sql2 = "insert into t_productclass(pc_name,parent_id,pc_order) values (?pc_name,?parent_id,?pc_order)"; params2 = new MySqlParameter[]{ new MySqlParameter("?pc_name",MySqlDbType.VarChar), new MySqlParameter("?parent_id",MySqlDbType.Int32), new MySqlParameter("?pc_order",MySqlDbType.Int32) }; params2[0].Value = myProductClass.PC_Name; params2[1].Value = parent_id; params2[2].Value = pc_order + 1; } result = DbHelper.ExecuteNonQuery(sql2, params2); return result > 0; }
public static bool Insert(ProductClass myProductClass) { if (!string.IsNullOrEmpty(myProductClass.PC_Name)) { return ProductClassService.Insert(myProductClass); } else { return false; } }
public static bool Update(int pc_id, string pc_name) { ProductClass myProductClass = new ProductClass(); if (pc_id > 0 && !string.IsNullOrEmpty(pc_name)) { myProductClass.PC_Id = pc_id; myProductClass.PC_Name = pc_name; return ProductClassService.Update(myProductClass); } else { return false; } }