数据读取扩展类

 1 /// <summary>
 2     /// 数据读取扩展类
 3     /// </summary>
 4     public static class DataReadExtend
 5     {
 6         /// <summary>
 7         /// 实体类转化
 8         /// </summary>
 9         /// <param name="DataReader">SqlDataReader</param>
10         /// <returns></returns>
11         public static List<T> ToList<T>(this SqlDataReader DataReader) where T : class
12         {
13             List<T> result = null;
14             Type type = typeof(T);
15             var method = type.GetProperties();
16             if (DataReader.HasRows)
17             {
18                 result = new List<T>();
19                 while (DataReader.Read())
20                 {
21                     var func = Activator.CreateInstance(type);
22                     foreach (PropertyInfo p in method)
23                     {
24                         if (DataReader[p.Name] == System.DBNull.Value)
25                         {
26                             continue;
27                         }
28                         else
29                             p.SetValue(func, DataReader[p.Name], null);
30                     }
31                     result.Add(func as T);
32                 }
33             }
34             return result;
35 
36         }
37         /// <summary>
38         /// 转换单个对象
39         /// </summary>
40         /// <param name="DataReader">DataReader</param>
41         /// <returns></returns>
42         public static T ToModel<T>(this SqlDataReader DataReader) where T : class
43         {
44             T result = default(T);
45             Type type = typeof(T);
46             var method = type.GetProperties();
47             if (DataReader.HasRows)
48             {
49 
50                 while (DataReader.Read())
51                 {
52                     var func = Activator.CreateInstance(type);
53                     foreach (PropertyInfo p in method)
54                     {
55                         if (DataReader[p.Name] == System.DBNull.Value)
56                         {
57                             continue;
58                         }
59                         else
60                             p.SetValue(func, DataReader[p.Name], null);
61                     }
62                     result = func as T;
63                 }
64             }
65             return result;
66 
67         }
68         /// <summary>
69         /// DataTable转成对象
70         /// </summary>
71         /// <param name="table"></param>
72         /// <returns></returns>
73         public static List<T> ToList<T>(this DataTable table) where T : class
74         {
75             List<T> result = null;
76             Type type = typeof(T);
77             if (table.Rows.Count > 0)
78             {
79                 result = new List<T>();
80                 foreach (DataRow item in table.Rows)
81                 {
82                     var c = Activator.CreateInstance(type);
83                     foreach (PropertyInfo p in c.GetType().GetProperties())
84                     {
85                         if (item[p.Name] == System.DBNull.Value)
86                         {
87                             continue;
88                         }
89                         else
90                             p.SetValue(c, item[p.Name], null);
91                     }
92                     result.Add(c as T);
93                 }
94             }
95             return result;
96 
97         }
98 
99     }

 

posted @ 2019-10-17 20:59  苍云者  阅读(68)  评论(0编辑  收藏  举报