博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataTable与实体类互相转换
阅读量:6149 次
发布时间:2019-06-21

本文共 3092 字,大约阅读时间需要 10 分钟。

  转自:http://www.cnblogs.com/dyfzwj/archive/2011/04/16/2017916.html

    /// <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();
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                    if (propertyInfo != null && dr[i] != DBNull.Value)
                        propertyInfo.SetValue(model, dr[i], 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();

            for (int i = 0; i < dr.Table.Columns.Count; i++)

            {
                PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                if (propertyInfo != null && dr[i] != DBNull.Value)
                    propertyInfo.SetValue(model,dr[i],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

    }

转载于:https://www.cnblogs.com/bjxingch/articles/6346533.html

你可能感兴趣的文章
Java程序员从笨鸟到菜鸟之(三)面向对象之封装,继承,多态(下)
查看>>
Linux修改ssh默认端口
查看>>
MDT2010学习(九),部署Win7 x86 企业版
查看>>
Ajax
查看>>
mysql链接不上错误
查看>>
翻转二叉树
查看>>
nginx模块开发学习
查看>>
我的友情链接
查看>>
使用rsync备份系统快照
查看>>
Python登录验证小程序脚本
查看>>
perlreftut--->3
查看>>
AD------权限分配---AGDLP原则
查看>>
KVM精简教程(八):虚拟机管理之虚拟机克隆
查看>>
MySql 5.7 远程访问配置
查看>>
win10下运行esxi customizer的小技巧
查看>>
IE8背景图占满
查看>>
Flume(NG)架构设计要点及配置实践
查看>>
Spark stdout日志乱码
查看>>
Win7不时出现“此Windows副本不是正版”提示解决方法
查看>>
第14章1节《MonkeyRunner源码剖析》 HierarchyViewer实现原理 1
查看>>