And the transition between the entity class DataTable

the using the System;
 the using the System.Collections.Generic;
 the using the System.Data;
 the using the System.Linq;
 the using the System.Reflection;
 the using the System.Text; 

namespace the Common 
{ 
    public  class ModelHandler <T> WHERE T: new new () 
    { 
        #region the DataTable conversion entity classes /// <Summary> /// fill an object list: a table with a first filling of the entity class DataSet
         /// </ Summary> /// <param name = "DS"> DataSet </ param> / // <Returns> </ Returns>public static

         
         
         
         
         List <T> FillModel (the DataSet DS) 
        { 
            IF (DS == null || ds.Tables [ 0 ] == null || ds.Tables [ 0 ] .Rows.Count == 0 ) 
            { 
                return  null ; 
            } 
            the else 
            { 
                return FillModel (ds.Tables [ 0 ]); 
            } 
        } 

        ///  <Summary>   
        /// object list filler: filling a first entity class with the index table DataSet
         ///  </ Summary>   
        public  static list <T> FillModel (the DataSet DS, int index)
        {
            IF (DS == null || ds.Tables.Count <|| ds.Tables index = [index] .Rows.Count == 0 ) 
            { 
                return  null ; 
            } 
            the else 
            { 
                return FillModel (ds.Tables [index]); 
            } 
        } 

        ///  <Summary>   
        /// charging target list: DataTable filled with entity class
         ///  </ Summary>   
        public  static 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 static 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 static 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 static 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 static 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
    }
}

 

Guess you like

Origin www.cnblogs.com/zhaoyl9/p/11096659.html