/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.site_channel model) { Model.site_channel oldModel = GetModel(model.id); //取得旧的频道数据 int siteNavParentId = new DAL.MySql.sites(databaseprefix).GetSiteNavId(model.site_id); //取得站点对应的导航ID if (siteNavParentId == 0) { return(false); } using (MySqlConnection conn = new MySqlConnection(DbHelperMySql.connectionString)) { conn.Open(); using (MySqlTransaction trans = conn.BeginTransaction()) { try { #region 修改频道表====================== StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <MySqlParameter> paras = new List <MySqlParameter>(); strSql.Append("update " + databaseprefix + "site_channel set "); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 //!pi.Name.Equals("channel_fields") if (!pi.Name.Equals("id") && !typeof(System.Collections.IList).IsAssignableFrom(pi.PropertyType)) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + "=@" + pi.Name + ","); //声明参数 paras.Add(new MySqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(" where id=@id "); paras.Add(new MySqlParameter("@id", model.id)); DbHelperMySql.ExecuteSql(conn, trans, strSql.ToString(), paras.ToArray()); #endregion //删除已移除扩展字段及频道数据表列 FieldDelete(conn, trans, model, oldModel); //编辑扩展字段及频道数据表 FieldUpdate(conn, trans, model, oldModel); #region 编辑对应的导航================== new DAL.MySql.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name, siteNavParentId, "channel_" + model.name, model.title, model.sort_id); new DAL.MySql.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_list", "channel_" + model.name + "_list"); //内容管理 new DAL.MySql.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_category", "channel_" + model.name + "_category"); //栏目类别 //评论导航菜单处理 if (model.is_comment > 0 && oldModel.is_comment == 0) { //如开启评论而以前关闭的需新增菜单 new DAL.MySql.navigation(databaseprefix).Add(conn, trans, "channel_" + model.name, "channel_" + model.name + "_comment", "评论管理", "article/comment_list.aspx", 103, model.id, "Show,View,Add,Edit,Delete"); } else if (model.is_comment == 0 && oldModel.is_comment > 0) { //如关闭评论而以前开启需删除菜单 new DAL.MySql.navigation(databaseprefix).Delete(conn, trans, "channel_" + oldModel.name + "_comment"); } else if (model.is_comment > 0) { //如都是开启状态则修改菜单 new DAL.MySql.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_comment", "channel_" + model.name + "_comment"); } #endregion trans.Commit();//提交事务 } catch { trans.Rollback();//回滚事务 return(false); } } } return(true); }
/// <summary> /// 增加一条数据及其子表 /// </summary> public int Add(Model.site_channel model) { //取得站点对应的导航,如果站点导航不存在则退出新增 int parent_id = new DAL.MySql.sites(databaseprefix).GetSiteNavId(model.site_id); if (parent_id == 0) { return(0); } using (MySqlConnection conn = new MySqlConnection(DbHelperMySql.connectionString)) { conn.Open(); //打开数据连接 using (MySqlTransaction trans = conn.BeginTransaction()) { try { #region 写入频道表数据================== StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //数据字段 StringBuilder str2 = new StringBuilder(); //数据参数 //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <MySqlParameter> paras = new List <MySqlParameter>(); strSql.Append("insert into " + databaseprefix + "site_channel("); foreach (PropertyInfo pi in pros) { //如果不是主键和List<T>类型则追加sql字符串 if (!pi.Name.Equals("id") && !typeof(System.Collections.IList).IsAssignableFrom(pi.PropertyType)) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + ","); //拼接字段 str2.Append("@" + pi.Name + ","); //声明参数 paras.Add(new MySqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(") values ("); strSql.Append(str2.ToString().Trim(',')); strSql.Append(") "); strSql.Append(";select @@IDENTITY;"); object obj = DbHelperMySql.GetSingle(conn, trans, strSql.ToString(), paras.ToArray());//带事务 model.id = Convert.ToInt32(obj); #endregion //写入扩展字段及创建频道数据表 FieldAdd(conn, trans, model); #region 写入导航数据=============== int newNavId = new DAL.MySql.navigation(databaseprefix).Add(conn, trans, parent_id.ToString(), "channel_" + model.name, model.title, "", model.sort_id, model.id, "Show"); new DAL.MySql.navigation(databaseprefix).Add(conn, trans, newNavId.ToString(), "channel_" + model.name + "_list", "内容管理", "article/article_list.aspx", 99, model.id, "Show,View,Add,Edit,Delete,Audit"); new DAL.MySql.navigation(databaseprefix).Add(conn, trans, newNavId.ToString(), "channel_" + model.name + "_category", "栏目类别", "article/category_list.aspx", 100, model.id, "Show,View,Add,Edit,Delete"); //开启评论则新增菜单 if (model.is_comment > 0) { new DAL.MySql.navigation(databaseprefix).Add(conn, trans, newNavId.ToString(), "channel_" + model.name + "_comment", "评论管理", "article/comment_list.aspx", 103, model.id, "Show,View,Delete,Reply"); } #endregion trans.Commit();//提交事务 } catch { trans.Rollback();//回滚事务 return(0); } } } return(model.id); }
public sites() { dal = new DAL.MySql.sites(sysConfig.sysdatabaseprefix); }