/// <summary> /// 是否存在该记录 /// </summary> public bool Exists(string column_name) { //检查是否与文章字段相同 Model.article artModel = new Model.article(); //利用反射获得属性的所有公共属性 Type modelType = artModel.GetType(); PropertyInfo[] proInfo = modelType.GetProperties(); foreach (PropertyInfo pi in proInfo) { if (pi.Name.ToLower() == column_name.ToLower()) { return(true); } } //检查是否与扩展字段表列相同 StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from " + databaseprefix + "article_attribute_field"); strSql.Append(" where name=@name "); SqlParameter[] parameters = { new SqlParameter("@name", SqlDbType.NVarChar, 100) }; parameters[0].Value = column_name; if (DbHelperSQL.Exists(strSql.ToString(), parameters)) { return(true); } return(false); }
/// <summary> /// 将对象转换实体 /// </summary> public Model.article DataRowToModel(DataRow row) { Model.article model = new Model.article(); if (row != null) { //利用反射获得属性的所有公共属性 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);//用索引值设置属性值 } } } return(model); }
/// <summary> /// 将对象转换为实体 /// </summary> private Model.article DataRowToModel(DataRow row) { Model.article model = new Model.article();//主表字段 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 扩展字段信息=================== Dictionary <string, string> fieldDic = new DAL.article_attribute_field(databaseprefix).GetFields(model.channel_id);//扩展字段字典 for (int i = 0; i < row.Table.Columns.Count; i++) { if (fieldDic.ContainsKey(row.Table.Columns[i].ColumnName) && row[i] != null) { fieldDic[row.Table.Columns[i].ColumnName] = row[i].ToString(); } } model.fields = fieldDic; #endregion //相册信息 model.albums = new DAL.article_albums(databaseprefix).GetList(model.channel_id, model.id); //附件信息 model.attach = new DAL.article_attach(databaseprefix).GetList(model.channel_id, model.id); //用户组价格 model.group_price = new DAL.user_group_price(databaseprefix).GetList(model.channel_id, model.id); } return(model); }
/// <summary> /// 查询是否存在列 /// </summary> public bool Exists(string column_name) { //检查是否与文章字段相同 Model.article artModel = new Model.article(); //利用反射获得属性的所有公共属性 Type modelType = artModel.GetType(); PropertyInfo[] proInfo = modelType.GetProperties(); foreach (PropertyInfo pi in proInfo) { if (pi.Name.ToLower() == column_name.ToLower()) { return(true); } } //检查是否与扩展字段表列相同 StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from " + databaseprefix + "article_attribute_field"); strSql.Append(" where name=@0 "); return(ReadDataBase.ExecuteScalar <int>(strSql.ToString(), column_name) > 0); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.article model) { //查询频道名称 string channelName = new DAL.site_channel(databaseprefix).GetChannelName(model.channel_id); if (channelName.Length == 0) { return(0); } using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { #region 添加主表数据==================== StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //数据字段 StringBuilder str2 = new StringBuilder(); //数据参数 //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("insert into " + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + channelName + "("); //主表字段信息 foreach (PropertyInfo pi in pros) { //如果不是主键或List<T>则追加sql字符串 if (!pi.Name.Equals("id") && !pi.Name.Equals("fields") && !typeof(System.Collections.IList).IsAssignableFrom(pi.PropertyType)) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + ","); //拼接字段 str2.Append("@" + pi.Name + ","); //声明参数 paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } //扩展字段信息 foreach (KeyValuePair <string, string> kvp in model.fields) { str1.Append(kvp.Key + ","); //拼接字段 str2.Append("@" + kvp.Key + ","); //声明参数 paras.Add(new SqlParameter("@" + kvp.Key, kvp.Value)); //对参数赋值 } strSql.Append(str1.ToString().Trim(',')); strSql.Append(") values ("); strSql.Append(str2.ToString().Trim(',')); strSql.Append(") "); strSql.Append(";select @@IDENTITY;"); object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), paras.ToArray()); //带事务 model.id = Convert.ToInt32(obj); //插入后赋值 #endregion #region 添加图片相册==================== if (model.albums != null) { new DAL.article_albums(databaseprefix).Add(conn, trans, model.albums, model.channel_id, model.id); } #endregion #region 添加内容附件==================== if (model.attach != null) { new DAL.article_attach(databaseprefix).Add(conn, trans, model.attach, model.channel_id, model.id); } #endregion #region 添加用户组价格================== if (model.group_price != null) { foreach (Model.user_group_price modelt in model.group_price) { new DAL.user_group_price(databaseprefix).Add(conn, trans, modelt, model.channel_id, model.id); } } #endregion #region 添加Tags标签==================== if (model.tags != null && model.tags.Trim().Length > 0) { string[] tagsArr = model.tags.Trim().Split(','); if (tagsArr.Length > 0) { foreach (string tagsStr in tagsArr) { new DAL.article_tags(databaseprefix).Update(conn, trans, tagsStr, model.channel_id, model.id); } } } #endregion trans.Commit(); } catch { trans.Rollback(); return(0); } } } return(model.id); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.article model) { //查询频道名称 string channelName = new DAL.site_channel(databaseprefix).GetChannelName(model.channel_id); if (channelName.Length == 0) { return(false); } using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { #region 修改主表数据========================== StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("update " + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + channelName + " set "); //主表字段信息 foreach (PropertyInfo pi in pros) { //如果不是主键或List<T>则追加sql字符串 if (!pi.Name.Equals("id") && !pi.Name.Equals("fields") && !typeof(System.Collections.IList).IsAssignableFrom(pi.PropertyType)) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + "=@" + pi.Name + ","); //声明参数 paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } //扩展字段信息 foreach (KeyValuePair <string, string> kvp in model.fields) { str1.Append(kvp.Key + "=@" + kvp.Key + ","); //声明参数 paras.Add(new SqlParameter("@" + kvp.Key, kvp.Value)); //对参数赋值 } strSql.Append(str1.ToString().Trim(',')); strSql.Append(" where id=@id"); paras.Add(new SqlParameter("@id", model.id)); DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), paras.ToArray()); #endregion #region 修改图片相册========================== //删除/添加/修改相册图片 new DAL.article_albums(databaseprefix).Update(conn, trans, model.albums, model.channel_id, model.id); #endregion #region 修改内容附件========================== //删除/添加/修改附件 new DAL.article_attach(databaseprefix).Update(conn, trans, model.attach, model.channel_id, model.id); #endregion #region 修改用户组价格======================== //删除旧用户组价格 new DAL.user_group_price(databaseprefix).Delete(conn, trans, model.channel_id, model.id); //添加用户组价格 if (model.group_price != null) { foreach (Model.user_group_price modelt in model.group_price) { new DAL.user_group_price(databaseprefix).Add(conn, trans, modelt, model.channel_id, model.id); } } #endregion #region 修改Tags标签========================== //删除已有的Tags标签关系 new DAL.article_tags(databaseprefix).Delete(conn, trans, model.channel_id, model.id); //添加添加标签 if (model.tags != null && model.tags.Trim().Length > 0) { string[] tagsArr = model.tags.Trim().Split(','); if (tagsArr.Length > 0) { foreach (string tagsStr in tagsArr) { new DAL.article_tags(databaseprefix).Update(conn, trans, tagsStr, model.channel_id, model.id); } } } #endregion trans.Commit(); } catch { trans.Rollback(); return(false); } } } return(true); }