DataSet、DataTable转换List(泛型集合与DataSet互相转换 )

  1 using System.Data;
  2 
  3 using System.Reflection;
  4 
  5 using System.Collections;
  6 
  7 using System.Collections.Generic;
  8 
  9 using System;
 10 
 11 namespace BI.ERP.DAL
 12 {
 13     public class DataSet转换List
 14     {
 15         /// <summary> 
 16 
 17         /// 泛型集合与DataSet互相转换 
 18 
 19         /// </summary> 
 20 
 21         public class IListDataSet
 22         {
 23             /// <summary> 
 24 
 25             /// 集合装换DataSet 
 26 
 27             /// </summary> 
 28 
 29             /// <param name="list">集合</param> 
 30 
 31             /// <returns></returns> 
 32 
 33             /// 2008-08-01 22:08 HPDV2806 
 34 
 35             public static DataSet ToDataSet(IList p_List)
 36             {
 37 
 38                 DataSet result = new DataSet();
 39 
 40                 DataTable _DataTable = new DataTable();
 41 
 42                 if (p_List.Count > 0)
 43                 {
 44 
 45                     PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
 46 
 47                     foreach (PropertyInfo pi in propertys)
 48                     {
 49 
 50                         _DataTable.Columns.Add(pi.Name, pi.PropertyType);
 51 
 52                     }
 53 
 54 
 55 
 56                     for (int i = 0; i < p_List.Count; i++)
 57                     {
 58 
 59                         ArrayList tempList = new ArrayList();
 60 
 61                         foreach (PropertyInfo pi in propertys)
 62                         {
 63 
 64                             object obj = pi.GetValue(p_List[i], null);
 65 
 66                             tempList.Add(obj);
 67 
 68                         }
 69 
 70                         object[] array = tempList.ToArray();
 71 
 72                         _DataTable.LoadDataRow(array, true);
 73 
 74                     }
 75 
 76                 }
 77 
 78                 result.Tables.Add(_DataTable);
 79 
 80                 return result;
 81 
 82             }
 83 
 84 
 85 
 86             /// <summary> 
 87 
 88             /// 泛型集合转换DataSet 
 89 
 90             /// </summary> 
 91 
 92             /// <typeparam name="T"></typeparam> 
 93 
 94             /// <param name="list">泛型集合</param> 
 95 
 96             /// <returns></returns> 
 97 
 98             /// 2008-08-01 22:43 HPDV2806 
 99 
100             public static DataSet ToDataSet<T>(IList<T> list)
101             {
102 
103                 return ToDataSet<T>(list, null);
104 
105             }
106 
107 
108 
109 
110 
111             /// <summary> 
112 
113             /// 泛型集合转换DataSet 
114 
115             /// </summary> 
116 
117             /// <typeparam name="T"></typeparam> 
118 
119             /// <param name="p_List">泛型集合</param> 
120 
121             /// <param name="p_PropertyName">待转换属性名数组</param> 
122 
123             /// <returns></returns> 
124 
125             /// 2008-08-01 22:44 HPDV2806 
126 
127             public static DataSet ToDataSet<T>(IList<T> p_List, params string[] p_PropertyName)
128             {
129 
130                 List<string> propertyNameList = new List<string>();
131 
132                 if (p_PropertyName != null)
133 
134                     propertyNameList.AddRange(p_PropertyName);
135 
136 
137 
138                 DataSet result = new DataSet();
139 
140                 DataTable _DataTable = new DataTable();
141 
142                 if (p_List.Count > 0)
143                 {
144 
145                     PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
146 
147                     foreach (PropertyInfo pi in propertys)
148                     {
149 
150                         if (propertyNameList.Count == 0)
151                         {
152 
153                             // 没有指定属性的情况下全部属性都要转换 
154 
155                             _DataTable.Columns.Add(pi.Name, pi.PropertyType);
156 
157                         }
158 
159                         else
160                         {
161 
162                             if (propertyNameList.Contains(pi.Name))
163 
164                                 _DataTable.Columns.Add(pi.Name, pi.PropertyType);
165 
166                         }
167 
168                     }
169 
170 
171 
172                     for (int i = 0; i < p_List.Count; i++)
173                     {
174 
175                         ArrayList tempList = new ArrayList();
176 
177                         foreach (PropertyInfo pi in propertys)
178                         {
179 
180                             if (propertyNameList.Count == 0)
181                             {
182 
183                                 object obj = pi.GetValue(p_List[i], null);
184 
185                                 tempList.Add(obj);
186 
187                             }
188 
189                             else
190                             {
191 
192                                 if (propertyNameList.Contains(pi.Name))
193                                 {
194 
195                                     object obj = pi.GetValue(p_List[i], null);
196 
197                                     tempList.Add(obj);
198 
199                                 }
200 
201                             }
202 
203                         }
204 
205                         object[] array = tempList.ToArray();
206 
207                         _DataTable.LoadDataRow(array, true);
208 
209                     }
210 
211                 }
212 
213                 result.Tables.Add(_DataTable);
214 
215                 return result;
216 
217             }
218 
219 
220 
221             /// <summary> 
222 
223             /// DataSet装换为泛型集合 
224 
225             /// </summary> 
226 
227             /// <typeparam name="T"></typeparam> 
228 
229             /// <param name="p_DataSet">DataSet</param> 
230 
231             /// <param name="p_TableIndex">待转换数据表索引</param> 
232 
233             /// <returns></returns> 
234 
235             /// 2008-08-01 22:46 HPDV2806 
236 
237             public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
238             {
239 
240                 if (p_DataSet == null || p_DataSet.Tables.Count < 0)
241 
242                     return null;
243 
244                 if (p_TableIndex > p_DataSet.Tables.Count - 1)
245 
246                     return null;
247 
248                 if (p_TableIndex < 0)
249 
250                     p_TableIndex = 0;
251 
252 
253 
254                 DataTable p_Data = p_DataSet.Tables[p_TableIndex];
255 
256                 // 返回值初始化 
257 
258                 IList<T> result = new List<T>();
259 
260                 for (int j = 0; j < p_Data.Rows.Count; j++)
261                 {
262 
263                     T _t = (T)Activator.CreateInstance(typeof(T));
264 
265                     PropertyInfo[] propertys = _t.GetType().GetProperties();
266 
267                     foreach (PropertyInfo pi in propertys)
268                     {
269 
270                         for (int i = 0; i < p_Data.Columns.Count; i++)
271                         {
272 
273                             // 属性与字段名称一致的进行赋值 
274 
275                             if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
276                             {
277 
278                                 // 数据库NULL值单独处理 
279 
280                                 if (p_Data.Rows[j][i] != DBNull.Value)
281 
282                                     pi.SetValue(_t, p_Data.Rows[j][i], null);
283 
284                                 else
285 
286                                     pi.SetValue(_t, null, null);
287 
288                                 break;
289 
290                             }
291 
292                         }
293 
294                     }
295 
296                     result.Add(_t);
297 
298                 }
299 
300                 return result;
301 
302             }
303 
304 
305 
306             /// <summary> 
307 
308             /// DataSet装换为泛型集合 
309 
310             /// </summary> 
311 
312             /// <typeparam name="T"></typeparam> 
313 
314             /// <param name="p_DataSet">DataSet</param> 
315 
316             /// <param name="p_TableName">待转换数据表名称</param> 
317 
318             /// <returns></returns> 
319 
320             /// 2008-08-01 22:47 HPDV2806 
321 
322             public static IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
323             {
324 
325                 int _TableIndex = 0;
326 
327                 if (p_DataSet == null || p_DataSet.Tables.Count < 0)
328 
329                     return null;
330 
331                 if (string.IsNullOrEmpty(p_TableName))
332 
333                     return null;
334 
335                 for (int i = 0; i < p_DataSet.Tables.Count; i++)
336                 {
337 
338                     // 获取Table名称在Tables集合中的索引值 
339 
340                     if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
341                     {
342 
343                         _TableIndex = i;
344 
345                         break;
346 
347                     }
348 
349                 }
350 
351                 return DataSetToIList<T>(p_DataSet, _TableIndex);
352 
353             }
354 
355             // <summary> 
356 
357             /// DataTable装换为泛型集合 
358 
359             /// </summary> 
360 
361             /// <typeparam name="T"></typeparam> 
362 
363             /// <param name="p_DataTable">p_DataTable</param> 
364 
365 
366             /// <returns></returns> 
367 
368             /// 2008-08-01 22:47 HPDV2806 
369             public static IList<T> DataTableToIList<T>(DataTable p_DataTable)
370             {
371 
372                 if (p_DataTable == null || p_DataTable.Rows.Count < 0)
373 
374                     return null;
375 
376                 //DataTable p_Data = p_DataSet.Tables[p_TableIndex];
377 
378                 // 返回值初始化 
379 
380                 IList<T> result = new List<T>();
381 
382                 for (int j = 0; j < p_DataTable.Rows.Count; j++)
383                 {
384 
385                     T _t = (T)Activator.CreateInstance(typeof(T));
386 
387                     PropertyInfo[] propertys = _t.GetType().GetProperties();
388 
389                     foreach (PropertyInfo pi in propertys)
390                     {
391 
392                         for (int i = 0; i < p_DataTable.Columns.Count; i++)
393                         {
394 
395                             // 属性与字段名称一致的进行赋值 
396 
397                             if (pi.Name.Equals(p_DataTable.Columns[i].ColumnName))
398                             {
399 
400                                 // 数据库NULL值单独处理 
401 
402                                 if (p_DataTable.Rows[j][i] != DBNull.Value)
403 
404                                     pi.SetValue(_t, p_DataTable.Rows[j][i], null);
405 
406                                 else
407 
408                                     pi.SetValue(_t, null, null);
409 
410                                 break;
411 
412                             }
413 
414                         }
415 
416                     }
417 
418                     result.Add(_t);
419 
420                 }
421 
422                 return result;
423 
424             }
425 
426         }
427     }
428 }
View Code

调用方法

  #region 

          List<Customer_model> CMList = DataSet转换List.IListDataSet.DataTableToIList<Customer_model>(da) as List<Customer_model>;

  #endregion
原文地址:https://www.cnblogs.com/shanshanlaichi/p/6553594.html