• DataTable与实体类互相转换


       /// <summary>
        /// DataTable与实体类互相转换
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        public class ModelHandler<T> where T : new()
        {
            #region DataTable转换成实体类
    
            /// <summary>
            /// 填充对象列表:用DataSet的第一个表填充实体类
            /// </summary>
            /// <param name="ds">DataSet</param>
            /// <returns></returns>
            public List<T> FillModel(DataSet ds)
            {
                if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    return FillModel(ds.Tables[0]);
                }
            }
    
            /// <summary>  
            /// 填充对象列表:用DataSet的第index个表填充实体类
            /// </summary>  
            public List<T> FillModel(DataSet ds, int index)
            {
                if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    return FillModel(ds.Tables[index]);
                }
            }
    
            /// <summary>  
            /// 填充对象列表:用DataTable填充实体类
            /// </summary>  
            public List<T> FillModel(DataTable dt)
            {
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;
                }
                List<T> modelList = new List<T>();
                foreach (DataRow dr in dt.Rows)
                {
                    //T model = (T)Activator.CreateInstance(typeof(T));  
                    T model = new T();
                    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                    {
                        object propertyVaule;
                        if (dr.IsNull(propertyInfo.Name))
                        {
                            propertyVaule = null;
                        }
                        else
                        {
                            propertyVaule = dr[propertyInfo.Name];
                        }
                        model.GetType().GetProperty(propertyInfo.Name).SetValue(model, propertyVaule, null);
                    }
                    modelList.Add(model);
                }
                return modelList;
            }
    
            /// <summary>  
            /// 填充对象:用DataRow填充实体类
            /// </summary>  
            public T FillModel(DataRow dr)
            {
                if (dr == null)
                {
                    return default(T);
                }
    
                //T model = (T)Activator.CreateInstance(typeof(T));  
                T model = new T();
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    object propertyVaule;
                    if (!dr.Table.Columns.Contains(propertyInfo.Name))
                    {
                        continue;
                    }
                    if (dr.IsNull(propertyInfo.Name))
                    {
                        propertyVaule = null;
                    }
                    else
                    {
                        propertyVaule = dr[propertyInfo.Name];
                    }
                    model.GetType().GetProperty(propertyInfo.Name).SetValue(model, propertyVaule, null);
                }
                return model;
            }
    
            #endregion
    
            #region 实体类转换成DataTable
    
            /// <summary>
            /// 实体类转换成DataSet
            /// </summary>
            /// <param name="modelList">实体类列表</param>
            /// <returns></returns>
            public DataSet FillDataSet(List<T> modelList)
            {
                if (modelList == null || modelList.Count == 0)
                {
                    return null;
                }
                else
                {
                    DataSet ds = new DataSet();
                    ds.Tables.Add(FillDataTable(modelList));
                    return ds;
                }
            }
    
            /// <summary>
            /// 实体类转换成DataTable
            /// </summary>
            /// <param name="modelList">实体类列表</param>
            /// <returns></returns>
            public DataTable FillDataTable(List<T> modelList)
            {
                if (modelList == null || modelList.Count == 0)
                {
                    return null;
                }
                DataTable dt = CreateData(modelList[0]);
    
                foreach (T model in modelList)
                {
                    DataRow dataRow = dt.NewRow();
                    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                    {
                        dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
                    }
                    dt.Rows.Add(dataRow);
                }
                return dt;
            }
    
            /// <summary>
            /// 根据实体类得到表结构
            /// </summary>
            /// <param name="model">实体类</param>
            /// <returns></returns>
            private DataTable CreateData(T model)
            {
                DataTable dataTable = new DataTable(typeof(T).Name);
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
                }
                return dataTable;
            }
    
            #endregion
        }

    相应调用

                            ModelHandler<PrimaryCustomerDTO> hander = new ModelHandler<PrimaryCustomerDTO>();
                            dto = hander.FillModel(dr);
  • 相关阅读:
    python基础练习题(题目 矩阵对角线之和)
    python基础练习题(题目 对10个数进行排序)
    python基础练习题(题目 文本颜色设置)
    windows批处理执行图片爬取脚本
    Linux 设置网卡最大传输单位MTU
    Linux 查看开机 log
    Linux实现脚本开机自启动
    查看 linux flash 分区大小查看 linux flash 分区大小
    kernel 编译提示 mkimage command not found – U-Boot images will not be built
    linux内核编译中No rule to make ... ipt_ecn.c 的处理
  • 原文地址:https://www.cnblogs.com/Jenny90/p/3613956.html
Copyright © 2020-2023  润新知