Ejemplo n.º 1
0
        /// <summary>
        /// 将对象转换实体
        /// </summary>
        public Dapper.Model.site_channel DataRowToModel(DataRow row)
        {
            Dapper.Model.site_channel model = new Dapper.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");
                SqlParameter[] parameters =
                {
                    new SqlParameter("@channel_id", SqlDbType.Int, 4)
                };
                parameters[0].Value = model.id;
                DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    int rowsCount = dt.Rows.Count;
                    List <Dapper.Model.site_channel_field> models = new List <Dapper.Model.site_channel_field>();
                    Dapper.Model.site_channel_field        modelt;
                    for (int n = 0; n < rowsCount; n++)
                    {
                        modelt = new Dapper.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);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 编辑扩展字段及频道数据表
 /// </summary>
 private void FieldUpdate(SqlConnection conn, SqlTransaction trans, Dapper.Model.site_channel newModel, Dapper.Model.site_channel oldModel)
 {
     if (newModel.channel_fields != null)
     {
         string newFieldIds = string.Empty; //用来存储新增的字段ID
         //添加扩展字段
         StringBuilder strSql1;
         foreach (Dapper.Model.site_channel_field modelt in newModel.channel_fields)
         {
             strSql1 = new StringBuilder();
             Dapper.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)");
                 SqlParameter[] parameters1 =
                 {
                     new SqlParameter("@channel_id", SqlDbType.Int, 4),
                     new SqlParameter("@field_id",   SqlDbType.Int, 4)
                 };
                 parameters1[0].Value = modelt.channel_id;
                 parameters1[1].Value = modelt.field_id;
                 DbHelperSQL.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 = DbHelperSQL.Query(conn, trans, strSql2.ToString());
             foreach (DataRow dr in ds.Tables[0].Rows)
             {
                 DbHelperSQL.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)
     {
         DbHelperSQL.ExecuteSql(conn, trans, "exec sp_rename '" + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + oldModel.name + "', '" + databaseprefix + DTKeys.TABLE_CHANNEL_ARTICLE + newModel.name + "'");
     }
 }