123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Reflection;
- namespace Ant.ORM
- {
- /// <summary>
- /// DataTable与实体类互相转换
- /// </summary>
- /// <typeparam name="T">实体类</typeparam>
- public class DataTableModelHelp<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())
- {
- model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], 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())
- {
- model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], 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
- }
- }
|