DataTable与实体类互相转换
Posted marblemm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataTable与实体类互相转换相关的知识,希望对你有一定的参考价值。
1 /// <summary> 2 /// DataTable与实体类互相转换 3 /// </summary> 4 /// <typeparam name="T">实体类</typeparam> 5 public class ModelHandler<T> where T : new() 6 { 7 #region DataTable转换成实体类 8 9 /// <summary> 10 /// 填充对象列表:用DataSet的第一个表填充实体类 11 /// </summary> 12 /// <param name="ds">DataSet</param> 13 /// <returns></returns> 14 public List<T> FillModel(DataSet ds) 15 { 16 if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0) 17 { 18 return null; 19 } 20 else 21 { 22 return FillModel(ds.Tables[0]); 23 } 24 } 25 26 /// <summary> 27 /// 填充对象列表:用DataSet的第index个表填充实体类 28 /// </summary> 29 public List<T> FillModel(DataSet ds, int index) 30 { 31 if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0) 32 { 33 return null; 34 } 35 else 36 { 37 return FillModel(ds.Tables[index]); 38 } 39 } 40 41 /// <summary> 42 /// 填充对象列表:用DataTable填充实体类 43 /// </summary> 44 public List<T> FillModel(DataTable dt) 45 { 46 if (dt == null || dt.Rows.Count == 0) 47 { 48 return null; 49 } 50 List<T> modelList = new List<T>(); 51 foreach (DataRow dr in dt.Rows) 52 { 53 //T model = (T)Activator.CreateInstance(typeof(T)); 54 T model = new T(); 55 for (int i = 0; i < dr.Table.Columns.Count; i++) 56 { 57 PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName); 58 if (propertyInfo != null && dr[i] != DBNull.Value) 59 propertyInfo.SetValue(model, dr[i], null); 60 } 61 62 modelList.Add(model); 63 } 64 return modelList; 65 } 66 67 /// <summary> 68 /// 填充对象:用DataRow填充实体类 69 /// </summary> 70 public T FillModel(DataRow dr) 71 { 72 if (dr == null) 73 { 74 return default(T); 75 } 76 77 //T model = (T)Activator.CreateInstance(typeof(T)); 78 T model = new T(); 79 80 for (int i = 0; i < dr.Table.Columns.Count; i++) 81 { 82 PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName); 83 if (propertyInfo != null && dr[i] != DBNull.Value) 84 propertyInfo.SetValue(model,dr[i],null); 85 } 86 return model; 87 } 88 89 #endregion 90 91 #region 实体类转换成DataTable 92 93 /// <summary> 94 /// 实体类转换成DataSet 95 /// </summary> 96 /// <param name="modelList">实体类列表</param> 97 /// <returns></returns> 98 public DataSet FillDataSet(List<T> modelList) 99 { 100 if (modelList == null || modelList.Count == 0) 101 { 102 return null; 103 } 104 else 105 { 106 DataSet ds = new DataSet(); 107 ds.Tables.Add(FillDataTable(modelList)); 108 return ds; 109 } 110 } 111 112 /// <summary> 113 /// 实体类转换成DataTable 114 /// </summary> 115 /// <param name="modelList">实体类列表</param> 116 /// <returns></returns> 117 public DataTable FillDataTable(List<T> modelList) 118 { 119 if (modelList == null || modelList.Count == 0) 120 { 121 return null; 122 } 123 DataTable dt = CreateData(modelList[0]); 124 125 foreach(T model in modelList) 126 { 127 DataRow dataRow = dt.NewRow(); 128 foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) 129 { 130 dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null); 131 } 132 dt.Rows.Add(dataRow); 133 } 134 return dt; 135 } 136 137 /// <summary> 138 /// 根据实体类得到表结构 139 /// </summary> 140 /// <param name="model">实体类</param> 141 /// <returns></returns> 142 private DataTable CreateData(T model) 143 { 144 DataTable dataTable = new DataTable(typeof (T).Name); 145 foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) 146 { 147 dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType)); 148 } 149 return dataTable; 150 } 151 152 #endregion 153 }
参考:
http://blog.csdn.net/caowei880123/article/details/6327412
http://www.cnblogs.com/dyfzwj/archive/2011/04/16/2017916.html
更详细介绍:http://www.cnblogs.com/HCCZX/archive/2012/07/26/2609389.html
以上是关于DataTable与实体类互相转换的主要内容,如果未能解决你的问题,请参考以下文章