zoukankan      html  css  js  c++  java
  • DataTable转为List对象

     1  public static List<T> Convert2Object<T>(DataTable dt) where T : new() 
     2         {
     3             List<T> list = new List<T>();
     4             foreach (DataRow dr in dt.Rows)
     5             {
     6                 list.Add(ToEntity<T>(dr));
     7             }
     8             return list;
     9         }
    10 
    11         public static T ToEntity<T>(DataRow dr) where T : new()
    12         {
    13             T model = new T();
    14             foreach (PropertyInfo pInfo in model.GetType().GetProperties())
    15             {
    16                 object val = GetValueByColumnName(dr, pInfo.Name);
    17                 pInfo.SetValue(model, val, null);
    18             }
    19             return model;
    20         }
    21 
    22         private static object GetValueByColumnName(System.Data.DataRow dr, string columnName)
    23         {
    24             if (dr.Table.Columns.IndexOf(columnName) >= 0)
    25             {
    26                 if (dr[columnName] == DBNull.Value)
    27                     return null;
    28                 return dr[columnName];
    29             }
    30             return null;
    31         }
  • 相关阅读:
    软件设计工具
    电脑运行 apk
    苹果开发网站
    在Tomcat中部署war
    sql server 2000 语法
    用 xml格式 输出 jsp
    点子网站
    网站推广
    TabWidget
    Java 中文拼音 排序
  • 原文地址:https://www.cnblogs.com/symcious/p/4854012.html
Copyright © 2011-2022 走看看