1     /// <summary>
  2     /// The data extension.
  3     /// </summary>
  4     public static class DataExtension
  5     {
  6         /// <summary>
  7         /// ToList
  8         /// </summary>
  9         /// <typeparam name="T">T</typeparam>
 10         /// <param name="reader">reader</param>
 11         /// <returns>T</returns>
 12         public static List<T> ToList<T>(this IDataReader reader) where T : class, new()
 13         {
 14             var result = new List<T>();
 15 
 16             DataTable dt = reader.GetSchemaTable();
 17             try
 18             {
 19                 while (reader.Read())
 20                 {
 21                     var t = new T();
 22 
 23                     if (dt != null)
 24                     {
 25                         foreach (DataRow dr in dt.Rows)
 26                         {
 27                             // 当前列名&属性名
 28                             string columnName = dr[0].ToString();
 29                             PropertyInfo pro = typeof(T).GetProperty(columnName);
 30 
 31                             if (pro == null)
 32                             {
 33                                 continue;
 34                             }
 35 
 36                             if (!pro.CanWrite)
 37                             {
 38                                 continue;
 39                             }
 40 
 41                             pro.SetValue(t, ConvertExtension.ConvertHelper(reader[columnName], pro.PropertyType), null);
 42                         }
 43                     }
 44 
 45                     result.Add(t);
 46                 }
 47             }
 48             catch (System.Exception ex)
 49             {
 50                 throw ex;
 51             }
 52             finally
 53             {
 54                 if (!reader.IsClosed)
 55                 {
 56                     reader.Dispose();
 57                     reader.Close();
 58                 }
 59             }
 60 
 61             return result;
 62         }
 63 
 64         /// <summary>
 65         /// ToList
 66         /// </summary>
 67         /// <typeparam name="T">T</typeparam>
 68         /// <param name="dt">dt</param>
 69         /// <returns>T</returns>
 70         public static List<T> ToList<T>(this DataTable dt) where T : class, new()
 71         {
 72             var result = new List<T>();
 73             foreach (DataRow dr in dt.Rows)
 74             {
 75                 var t = new T();
 76                 try
 77                 {
 78                     foreach (DataColumn column in dt.Columns)
 79                     {
 80                         // 当前列名&属性名
 81                         string columnName = column.ColumnName;
 82                         PropertyInfo pro = typeof(T).GetProperty(columnName);
 83 
 84                         if (pro == null)
 85                         {
 86                             continue;
 87                         }
 88 
 89                         if (!pro.CanWrite)
 90                         {
 91                             continue;
 92                         }
 93 
 94                         pro.SetValue(t, ConvertExtension.ConvertHelper(dr[columnName], pro.PropertyType), null);
 95                     }
 96                 }
 97                 catch (System.Exception ex)
 98                 {
 99                     throw ex;
100                 }
101 
102                 result.Add(t);
103             }
104 
105             return result;
106         }
107 
108         /// <summary>
109         /// ToList
110         /// </summary>
111         /// <typeparam name="T">T</typeparam>
112         /// <param name="ds">ds</param>
113         /// <returns>T</returns>
114         public static List<T> ToList<T>(this DataSet ds) where T : class, new()
115         {
116             return ds.Tables[0].ToList<T>();
117         }
118 
119         /// <summary>
120         /// ToList
121         /// </summary>
122         /// <typeparam name="T">T</typeparam>
123         /// <param name="ds">ds</param>
124         /// <param name="dataTableIndex">dataTableIndex</param>
125         /// <returns>T</returns>
126         public static List<T> ToList<T>(this DataSet ds, int dataTableIndex) where T : class, new()
127         {
128             return ds.Tables[dataTableIndex].ToList<T>();
129         }
130 
131         /// <summary>
132         /// ToModel
133         /// </summary>
134         /// <typeparam name="T">T</typeparam>
135         /// <param name="reader">reader</param>
136         /// <returns>T</returns>
137         public static T ToModel<T>(this IDataReader reader) where T : class, new()
138         {
139             var t = new T();
140             DataTable dt = reader.GetSchemaTable();
141             try
142             {
143                 while (reader.Read())
144                 {
145                     if (dt != null)
146                     {
147                         foreach (DataRow dr in dt.Rows)
148                         {
149                             // 当前列名&属性名
150                             string columnName = dr[0].ToString();
151                             PropertyInfo pro = typeof(T).GetProperty(columnName);
152 
153                             if (pro == null)
154                             {
155                                 continue;
156                             }
157 
158                             if (!pro.CanWrite)
159                             {
160                                 continue;
161                             }
162 
163                             pro.SetValue(t, ConvertExtension.ConvertHelper(reader[columnName], pro.PropertyType), null);
164                         }
165                     }
166                 }
167             }
168             catch (System.Exception ex)
169             {
170                 throw ex;
171             }
172             finally
173             {
174                 if (!reader.IsClosed)
175                 {
176                     reader.Dispose();
177                     reader.Close();
178                 }
179             }
180 
181             return t;
182         }
183 
184         /// <summary>
185         /// ToModel
186         /// </summary>
187         /// <typeparam name="T">T</typeparam>
188         /// <param name="dt">dt</param>
189         /// <returns>T</returns>
190         public static T ToModel<T>(this DataTable dt) where T : class, new()
191         {
192             var t = new T();
193             if (dt.Rows.Count <= 0)
194             {
195                 return t;
196             }
197 
198             try
199             {
200                 foreach (DataColumn column in dt.Columns)
201                 {
202                     // 当前列名&属性名
203                     string columnName = column.ColumnName;
204                     PropertyInfo pro = typeof(T).GetProperty(columnName);
205                     if (pro == null)
206                     {
207                         continue;
208                     }
209 
210                     if (!pro.CanWrite)
211                     {
212                         continue;
213                     }
214 
215                     pro.SetValue(t, ConvertExtension.ConvertHelper(dt.Rows[0][columnName], pro.PropertyType), null);
216                 }
217             }
218             catch (System.Exception ex)
219             {
220                 throw ex;
221             }
222 
223             return t;
224         }
225 
226         /// <summary>
227         /// ToModel
228         /// </summary>
229         /// <typeparam name="T">T</typeparam>
230         /// <param name="ds">ds</param>
231         /// <param name="dataTableIndex">dataTableIndex</param>
232         /// <returns>T</returns>
233         public static T ToModel<T>(this DataSet ds, int dataTableIndex = 0) where T : class, new()
234         {
235             return ds.Tables[0].ToModel<T>();
236         }
237     }
DataExtension

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-06-06
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案