/// <summary> /// 将对象转换实体 /// </summary> public Model.site_channel DataRowToModel(DataRow row) { Model.site_channel model = new Model.site_channel(); if (row != null) { #region 主表信息====================== //利用反射获得属性的所有公共属性 Type modelType = model.GetType(); for (int i = 0; i < row.Table.Columns.Count; i++) { PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName); if (proInfo != null && row[i] != DBNull.Value) { proInfo.SetValue(model, row[i], null);//用索引值设置属性值 } } #endregion #region 子表信息====================== StringBuilder strSql = new StringBuilder(); strSql.Append("select * from " + databaseprefix + "site_channel_field"); strSql.Append(" where channel_id=@channel_id"); MySqlParameter[] parameters = { new MySqlParameter("@channel_id", MySqlDbType.Int32, 4) }; parameters[0].Value = model.id; DataTable dt = DbHelperMySql.Query(strSql.ToString(), parameters).Tables[0]; if (dt.Rows.Count > 0) { int rowsCount = dt.Rows.Count; List <Model.site_channel_field> models = new List <Model.site_channel_field>(); Model.site_channel_field modelt; for (int n = 0; n < rowsCount; n++) { modelt = new Model.site_channel_field(); Type modeltType = modelt.GetType(); for (int i = 0; i < dt.Rows[n].Table.Columns.Count; i++) { PropertyInfo proInfo = modeltType.GetProperty(dt.Rows[n].Table.Columns[i].ColumnName); if (proInfo != null && dt.Rows[n][i] != DBNull.Value) { proInfo.SetValue(modelt, dt.Rows[n][i], null); } } models.Add(modelt); } model.channel_fields = models; } #endregion } return(model); }
/// <summary> /// 编辑扩展字段及频道数据表 /// </summary> private void FieldUpdate(MySqlConnection conn, MySqlTransaction trans, Model.site_channel newModel, Model.site_channel oldModel) { if (newModel.channel_fields != null) { string newFieldIds = string.Empty; //用来存储新增的字段ID //添加扩展字段 StringBuilder strSql1; foreach (Model.site_channel_field modelt in newModel.channel_fields) { strSql1 = new StringBuilder(); Model.site_channel_field fieldModel = null; if (oldModel.channel_fields != null) { fieldModel = oldModel.channel_fields.Find(p => p.field_id == modelt.field_id); //查找是否已经存在 } if (fieldModel == null) //如果不存在则添加 { newFieldIds += modelt.field_id + ","; //以逗号分隔开存储 strSql1.Append("insert into " + databaseprefix + "site_channel_field("); strSql1.Append("channel_id,field_id)"); strSql1.Append(" values ("); strSql1.Append("@channel_id,@field_id)"); MySqlParameter[] parameters1 = { new MySqlParameter("@channel_id", MySqlDbType.Int32, 4), new MySqlParameter("@field_id", MySqlDbType.Int32, 4) }; parameters1[0].Value = modelt.channel_id; parameters1[1].Value = modelt.field_id; DbHelperMySql.ExecuteSql(conn, trans, strSql1.ToString(), parameters1); } } //添加频道数据表列 if (newFieldIds.Length > 0) { StringBuilder strSql2 = new StringBuilder(); strSql2.Append("select id,[name],data_type from " + databaseprefix + "article_attribute_field"); strSql2.Append(" where id in(" + newFieldIds.TrimEnd(',') + ")"); DataSet ds = DbHelperMySql.Query(conn, trans, strSql2.ToString()); foreach (DataRow dr in ds.Tables[0].Rows) { DbHelperMySql.ExecuteSql(conn, trans, "alter table " + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + oldModel.name + " add " + dr["name"].ToString() + " " + dr["data_type"].ToString()); } } } //如果频道名称改变则需要更改数据表名 if (newModel.name != oldModel.name) { DbHelperMySql.ExecuteSql(conn, trans, "exec sp_rename '" + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + oldModel.name + "', '" + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + newModel.name + "'"); } }
private void ShowInfo(int _id) { BLL.site_channel bll = new BLL.site_channel(); Model.site_channel model = bll.GetModel(_id); txtTitle.Text = model.title; txtName.Text = model.name; txtName.Focus(); //设置焦点,防止JS无法提交 txtName.Attributes.Add("ajaxurl", "../../tools/admin_ajax.ashx?action=channel_name_validate&old_channel_name=" + Utils.UrlEncode(model.name)); ddlSiteId.SelectedValue = model.site_id.ToString(); if (model.is_lock == 1) { cbIsLock.Checked = false; } if (model.is_comment == 1) { cbIsComment.Checked = true; } if (model.is_albums == 1) { cbIsAlbums.Checked = true; } if (model.is_attach == 1) { cbIsAttach.Checked = true; } if (model.is_spec == 1) { cbIsSpec.Checked = true; } txtSortId.Text = model.sort_id.ToString(); //赋值扩展字段 if (model.channel_fields != null) { for (int i = 0; i < cblAttributeField.Items.Count; i++) { string[] fieldIdArr = cblAttributeField.Items[i].Value.Split(','); //分解出ID值 Model.site_channel_field modelt = model.channel_fields.Find(p => p.field_id == int.Parse(fieldIdArr[1])); //查找对应的字段ID if (modelt != null) { cblAttributeField.Items[i].Selected = true; } } } //绑定URL配置列表 rptList.DataSource = new BLL.url_rewrite().GetList(model.name); rptList.DataBind(); }
/// <summary> /// 编辑扩展字段及频道数据表 /// </summary> private void FieldUpdate(IDbConnection conn, IDbTransaction trans, Model.site_channel newModel, Model.site_channel oldModel) { if (newModel.channel_fields != null) { string newFieldIds = string.Empty; //用来存储新增的字段ID //添加扩展字段 StringBuilder strSql1; foreach (Model.site_channel_field modelt in newModel.channel_fields) { strSql1 = new StringBuilder(); Model.site_channel_field fieldModel = null; if (oldModel.channel_fields != null) { fieldModel = oldModel.channel_fields.Find(p => p.field_id == modelt.field_id); //查找是否已经存在 } if (fieldModel == null) //如果不存在则添加 { newFieldIds += modelt.field_id + ","; //以逗号分隔开存储 strSql1.Append("insert into " + databaseprefix + "site_channel_field("); strSql1.Append("channel_id,field_id)"); strSql1.Append(" values ("); strSql1.Append("@0,@1)"); WriteDataBase.Execute(conn, trans, strSql1.ToString(), modelt.channel_id, modelt.field_id); } } //添加频道数据表列 if (newFieldIds.Length > 0) { List <Model.article_attribute_field> field = new List <Model.article_attribute_field>(); field = WriteDataBase.Query <Model.article_attribute_field>(conn, trans, Sql.Builder.Select("id,name,data_type").From(databaseprefix + "article_attribute_field").Where("id in(" + newFieldIds.TrimEnd(',') + ")")).ToList(); foreach (var dr in field) { ReadDataBase.Execute(conn, trans, "alter table " + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + oldModel.name + " add " + dr.name + " " + dr.data_type); //(!string.IsNullOrEmpty(dr.default_value) ? "DEFAULT "+ dr.default_value : "") } } } //如果频道名称改变则需要更改数据表名 if (newModel.name != oldModel.name) { ReadDataBase.Execute(conn, trans, "exec sp_rename '" + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + oldModel.name + "', '" + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + newModel.name + "'"); } }
/// <summary> /// 将对象转换实体 /// </summary> public Model.site_channel DataRowToModel(DataRow row) { Model.site_channel model = new Model.site_channel(); if (row != null) { #region 主表信息====================== //利用反射获得属性的所有公共属性 Type modelType = model.GetType(); for (int i = 0; i < row.Table.Columns.Count; i++) { PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName); if (proInfo != null && row[i] != DBNull.Value) { proInfo.SetValue(model, row[i], null);//用索引值设置属性值 } } #endregion #region 子表信息====================== StringBuilder strSql = new StringBuilder(); strSql.Append("select * from " + databaseprefix + "site_channel_field"); strSql.Append(" where channel_id=" + model.id); DataTable dt = ReadDataBase.QueryFillDataSet(strSql.ToString()).Tables[0]; if (dt.Rows.Count > 0) { int rowsCount = dt.Rows.Count; List <Model.site_channel_field> models = new List <Model.site_channel_field>(); Model.site_channel_field modelt; for (int n = 0; n < rowsCount; n++) { modelt = new Model.site_channel_field(); Type modeltType = modelt.GetType(); for (int i = 0; i < dt.Rows[n].Table.Columns.Count; i++) { PropertyInfo proInfo = modeltType.GetProperty(dt.Rows[n].Table.Columns[i].ColumnName); if (proInfo != null && dt.Rows[n][i] != DBNull.Value) { proInfo.SetValue(modelt, dt.Rows[n][i], null); } } models.Add(modelt); } model.channel_fields = models; } #endregion #region 缩略图尺寸==================== StringBuilder strSql3 = new StringBuilder(); strSql3.Append("select id,title,class_id,channel_id,width,height,typeid,is_lock,add_time from " + databaseprefix + "site_channel_thum"); strSql3.Append(" where channel_id=" + model.id); DataTable ds3 = ReadDataBase.QueryFillDataSet(strSql3.ToString()).Tables[0]; if (ds3.Rows.Count > 0) { int i = ds3.Rows.Count; List <Model.site_channel_thum> models = new List <Model.site_channel_thum>(); Model.site_channel_thum modelt; for (int n = 0; n < i; n++) { modelt = new Model.site_channel_thum(); Type modeltType = modelt.GetType(); for (int j = 0; j < ds3.Rows[n].Table.Columns.Count; j++) { PropertyInfo proInfo = modeltType.GetProperty(ds3.Rows[n].Table.Columns[j].ColumnName); if (proInfo != null && ds3.Rows[n][j] != DBNull.Value) { proInfo.SetValue(modelt, ds3.Rows[n][j], null); } } models.Add(modelt); } model.channel_thums = models; } #endregion } return(model); }