zoukankan      html  css  js  c++  java
  • DataTable转换成实体类、List、DataSet等

     public partial class CommonService<T>
                         where T : class,
                         new()
        {
            /// <summary>
            /// DataTable与实体类互相转换
            /// </summary>
            /// <typeparam name="T">实体类</typeparam>
    
            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;
            }
    
            /// <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();
                    DataTable dt = new DataTable();
                    dt = FillDataTable(modelList);
                    ds.Tables.Add();
                    return ds;
                }
            }
            /// <summary>
            /// List转换成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>
            public DataTable CreateData(T t)
            {
                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;
            }
        }

    代码如上

    转自:

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

  • 相关阅读:
    Elasticsearch5.x批量插入数据(Java)
    Elasticsearch5.x创建索引(Java)
    Kibana5.x界面简要介绍(含x-pack插件)
    CentOS6.5安装Kibana5.3.0
    Elasticsearch5.x Head插件安装
    CentOS6.5安装Elasticsearch5.3.0
    jenkins发布Spring Cloud(单机版)
    prometheus 监控docker
    gitlab迁移单个项目
    python使用pika操作rabbitmq
  • 原文地址:https://www.cnblogs.com/wangjiaxiaoxi/p/6428486.html
Copyright © 2011-2022 走看看