• 实体类与DataTable互换


     /// <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;
            }
     /// <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>
            /// 将IDataReader类型转换为DataTable类型
            /// </summary>
            /// <param name="reader">IDataReader类型参数</param>
            /// <returns>DataTable</returns>
            public static DataTable GetDataTableFromIDataReader(IDataReader reader)
            {
                DataTable dt = new DataTable();
                bool init = false;
                dt.BeginLoadData();
                object[] vals = new object[0];
                while (reader.Read())
                {
                    if (!init)
                    {
                        init = true;
                        int fieldCount = reader.FieldCount;
                        for (int i = 0; i < fieldCount; ++i)
                        {
                            dt.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
                        }
                        vals = new object[fieldCount - 1];
                    }
                    reader.GetValues(vals);
                    dt.LoadDataRow(vals, true);
                }
                reader.Close();
                dt.EndLoadData();
                return dt;
            }
  • 相关阅读:
    java 读取配置文件
    oracle sql 截取表中某一字段的部分作为该字段查询结果
    大数据课堂
    网站保存
    Tensorflow+Keras 深度学习人工智能实践应用 Chapter Two 深度学习原理
    Tensorflow+Keras 深度学习人工智能实践应用 Chapter One人工智能 机器学习与深度学习简介
    Python 机器学习及实践 Coding 无监督学习经典模型 数据聚类 and 特征降维
    博客地址的保存
    备注
    个人笔记-
  • 原文地址:https://www.cnblogs.com/ypyhy/p/6888335.html
Copyright © 2020-2023  润新知