private bool DoEdit(int _id) { BLL.plugin_menu bll = new BLL.plugin_menu(); Model.plugin_menu model = bll.GetModel(_id); model.title = txtName.Text; model.link_url = txtUrl.Text; int parentId = int.Parse(ddlParentId.SelectedValue); if (parentId != model.id) { model.parent_id = parentId; } model.target = rblMode.SelectedValue; model.is_lock = int.Parse(rblHide.SelectedValue); model.sort_id = Utils.StrToInt(txtSort.Text, 99); model.css_txt = txtCssTxt.Text; model.img_url = txtImgUrl.Text.Trim(); if (bll.Update(model)) { AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改菜单内容:" + model.title); //记录日志 return(true); } return(false); }
/// <summary> /// 组合成对象实体 /// </summary> /// <param name="row">一行数据</param> /// <returns>Model.plugin_menu</returns> private Model.plugin_menu DataRowToModel(DataRow row) { Model.plugin_menu model = new Model.plugin_menu(); if (row != null) { if (null != row["id"] && "" != row["id"].ToString()) { model.id = int.Parse(row["id"].ToString()); } if (null != row["class_id"] && "" != row["class_id"].ToString()) { model.class_id = int.Parse(row["class_id"].ToString()); } if (null != row["title"]) { model.title = row["title"].ToString(); } if (null != row["link_url"]) { model.link_url = row["link_url"].ToString(); } if (null != row["parent_id"] && "" != row["parent_id"].ToString()) { model.parent_id = int.Parse(row["parent_id"].ToString()); } if (null != row["class_list"]) { model.class_list = row["class_list"].ToString(); } if (null != row["class_layer"] && "" != row["class_layer"].ToString()) { model.class_layer = int.Parse(row["class_layer"].ToString()); } if (null != row["target"]) { model.target = row["target"].ToString(); } if (null != row["sort_id"] && "" != row["sort_id"].ToString()) { model.sort_id = int.Parse(row["sort_id"].ToString()); } if (null != row["css_txt"]) { model.css_txt = row["css_txt"].ToString(); } if (null != row["img_url"]) { model.img_url = row["img_url"].ToString(); } if (null != row["is_lock"] && "" != row["is_lock"].ToString()) { model.is_lock = int.Parse(row["is_lock"].ToString()); } if (null != row["add_time"] && "" != row["add_time"].ToString()) { model.add_time = DateTime.Parse(row["add_time"].ToString()); } } return(model); }
private void ShowInfo(int _id) { BLL.plugin_menu bll = new BLL.plugin_menu(); Model.plugin_menu model = bll.GetModel(_id); txtName.Text = model.title; txtUrl.Text = model.link_url; ddlParentId.SelectedValue = model.parent_id.ToString(); txtSort.Text = model.sort_id.ToString(); rblHide.SelectedValue = model.is_lock.ToString(); rblMode.SelectedValue = model.target; txtCssTxt.Text = model.css_txt; txtImgUrl.Text = model.img_url; if (!string.IsNullOrEmpty(model.img_url)) { ImgDiv.Visible = true; ImgUrl.ImageUrl = model.img_url; } }
/// <summary> /// 修改子节点的ID列表及深度(自身迭代) /// </summary> /// <param name="parent_id">父类ID</param> private void UpdateChilds(SqlConnection conn, SqlTransaction trans, int parent_id) { //查找父节点信息 Model.plugin_menu model = GetModel(conn, trans, parent_id); if (null != model) { //查找子节点 string strSql = "select id from [" + databaseprefix + "plugin_menu] where parent_id=" + parent_id; DataSet ds = DbHelperSQL.Query(conn, trans, strSql); //带事务 foreach (DataRow dr in ds.Tables[0].Rows) { //修改子节点的ID列表及深度 int id = int.Parse(dr["id"].ToString()); string class_list = model.class_list + id + ","; int class_layer = model.class_layer + 1; DbHelperSQL.ExecuteSql(conn, trans, "update [" + databaseprefix + "plugin_menu] set class_list='" + class_list + "', class_layer=" + class_layer + " where id=" + id); //带事务 //调用自身迭代 this.UpdateChilds(conn, trans, id); //带事务 } } }
private bool DoAdd() { BLL.plugin_menu bll = new BLL.plugin_menu(); Model.plugin_menu model = new Model.plugin_menu(); model.class_id = this.class_id; model.title = txtName.Text; model.link_url = txtUrl.Text; model.parent_id = int.Parse(ddlParentId.SelectedValue); model.target = rblMode.SelectedValue; model.is_lock = int.Parse(rblHide.SelectedValue); model.sort_id = Utils.StrToInt(txtSort.Text, 99); model.css_txt = txtCssTxt.Text; model.img_url = txtImgUrl.Text.Trim(); model.add_time = DateTime.Now; if (bll.Add(model) > 0) { AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "添加菜单内容:" + model.title); //记录日志 return(true); } return(false); }
/// <summary> /// 更新一条数据 /// </summary> /// <param name="model">Model.plugin_menu</param> /// <returns>True Or False</returns> public bool Update(Model.plugin_menu model) { return(dal.Update(model)); }
/// <summary> /// 增加一条数据 /// </summary> /// <param name="model">Model.plugin_menu</param> /// <returns>ID</returns> public int Add(Model.plugin_menu model) { return(dal.Add(model)); }
/// <summary> /// 更新一条数据 /// </summary> /// <param name="model">Model.plugin_menu</param> /// <returns>True or False</returns> public bool Update(Model.plugin_menu model) { using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { //先判断选中的父节点是否被包含 if (IsContainNode(model.id, model.parent_id)) { //查找旧数据 Model.plugin_menu oldModel = GetModel(model.id); //查找旧父节点数据 string class_list = "," + model.parent_id + ","; int class_layer = 1; if (oldModel.parent_id > 0) { Model.plugin_menu oldParentModel = GetModel(conn, trans, oldModel.parent_id); //带事务 class_list = oldParentModel.class_list + model.parent_id + ","; class_layer = oldParentModel.class_layer + 1; } //先提升选中的父节点 DbHelperSQL.ExecuteSql(conn, trans, "update [" + databaseprefix + "plugin_menu] set parent_id=" + oldModel.parent_id + ",class_list='" + class_list + "', class_layer=" + class_layer + " where id=" + model.parent_id); //带事务 UpdateChilds(conn, trans, model.parent_id); //带事务 } //更新子节点 if (model.parent_id > 0) { Model.plugin_menu model2 = GetModel(conn, trans, model.parent_id); //带事务 model.class_list = model2.class_list + model.id + ","; model.class_layer = model2.class_layer + 1; } else { model.class_list = "," + model.id + ","; model.class_layer = 1; } StringBuilder strSql = new StringBuilder(); strSql.Append("update [" + databaseprefix + "plugin_menu] set "); strSql.Append("class_id=@class_id,"); strSql.Append("title=@title,"); strSql.Append("link_url=@link_url,"); strSql.Append("parent_id=@parent_id,"); strSql.Append("class_list=@class_list,"); strSql.Append("class_layer=@class_layer,"); strSql.Append("target=@target,"); strSql.Append("sort_id=@sort_id,"); strSql.Append("css_txt=@css_txt,"); strSql.Append("img_url=@img_url,"); strSql.Append("is_lock=@is_lock,"); strSql.Append("add_time=@add_time"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@class_id", SqlDbType.Int, 4), new SqlParameter("@title", SqlDbType.NVarChar, 255), new SqlParameter("@link_url", SqlDbType.NVarChar, 255), new SqlParameter("@parent_id", SqlDbType.Int, 4), new SqlParameter("@class_list", SqlDbType.NVarChar, 500), new SqlParameter("@class_layer", SqlDbType.Int, 4), new SqlParameter("@target", SqlDbType.NVarChar, 20), new SqlParameter("@sort_id", SqlDbType.Int, 4), new SqlParameter("@css_txt", SqlDbType.NVarChar, 50), new SqlParameter("@img_url", SqlDbType.NVarChar, 200), new SqlParameter("@is_lock", SqlDbType.TinyInt, 1), new SqlParameter("@add_time", SqlDbType.DateTime), new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = model.class_id; parameters[1].Value = model.title; parameters[2].Value = model.link_url; parameters[3].Value = model.parent_id; parameters[4].Value = model.class_list; parameters[5].Value = model.class_layer; parameters[6].Value = model.target; parameters[7].Value = model.sort_id; parameters[8].Value = model.css_txt; parameters[9].Value = model.img_url; parameters[10].Value = model.is_lock; parameters[11].Value = model.add_time; parameters[12].Value = model.id; DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters); //更新子节点 UpdateChilds(conn, trans, model.id); trans.Commit(); } catch (Exception ex) { trans.Rollback(); return(false); throw ex; } } } return(true); }
/// <summary> /// 增加一条数据 /// </summary> /// <param name="model">Model.plugin_menu</param> /// <returns>ID</returns> public int Add(Model.plugin_menu model) { using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into [" + databaseprefix + "plugin_menu]("); strSql.Append("class_id,title,link_url,parent_id,class_list,class_layer,target,sort_id,css_txt,img_url,is_lock,add_time"); strSql.Append(") values("); strSql.Append("@class_id,@title,@link_url,@parent_id,@class_list,@class_layer,@target,@sort_id,@css_txt,@img_url,@is_lock,@add_time)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@class_id", SqlDbType.Int, 4), new SqlParameter("@title", SqlDbType.NVarChar, 255), new SqlParameter("@link_url", SqlDbType.NVarChar, 255), new SqlParameter("@parent_id", SqlDbType.Int, 4), new SqlParameter("@class_list", SqlDbType.NVarChar, 500), new SqlParameter("@class_layer", SqlDbType.Int, 4), new SqlParameter("@target", SqlDbType.NVarChar, 20), new SqlParameter("@sort_id", SqlDbType.Int, 4), new SqlParameter("@css_txt", SqlDbType.NVarChar, 50), new SqlParameter("@img_url", SqlDbType.NVarChar, 200), new SqlParameter("@is_lock", SqlDbType.TinyInt, 1), new SqlParameter("@add_time", SqlDbType.DateTime) }; parameters[0].Value = model.class_id; parameters[1].Value = model.title; parameters[2].Value = model.link_url; parameters[3].Value = model.parent_id; parameters[4].Value = model.class_list; parameters[5].Value = model.class_layer; parameters[6].Value = model.target; parameters[7].Value = model.sort_id; parameters[8].Value = model.css_txt; parameters[9].Value = model.img_url; parameters[10].Value = model.is_lock; parameters[11].Value = model.add_time; object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务 model.id = Convert.ToInt32(obj); if (model.parent_id > 0) { Model.plugin_menu model2 = GetModel(conn, trans, model.parent_id); //带事务 model.class_list = model2.class_list + model.id + ","; model.class_layer = model2.class_layer + 1; } else { model.class_list = "," + model.id + ","; model.class_layer = 1; } //修改节点列表和深度 DbHelperSQL.ExecuteSql(conn, trans, "update [" + databaseprefix + "plugin_menu] set class_list='" + model.class_list + "', class_layer=" + model.class_layer + " where id=" + model.id); //带事务 trans.Commit(); } catch { trans.Rollback(); return(0); } } } return(model.id); }