zoukankan      html  css  js  c++  java
  • 把数据库中取出的DataTable转换成一个对象 或者对象列表

            public static void SetTValue<T>(T model, DataTable dt)
            {
                if (dt.Rows.Count == 0)
                {
                    return;
                }
                Type type = typeof(T);
                DataRow dr = dt.Rows[0];
                foreach (DataColumn dc in dt.Columns)
                {
                    PropertyInfo pi = type.GetProperty(dc.ColumnName);
                    if (pi == null)
                    {
                        continue;
                    }
    
                    if (dr[dc.ColumnName] != DBNull.Value)
                    {
                        pi.SetValue(model, dr[dc.ColumnName], null);
                    }
                }
            }
            public static List<T> SetTListValue<T>(DataTable dt) where T : new()
            {
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                List<T> TList = new List<T>();
                Type type = typeof(T);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    T t = new T();
                    DataRow dr = dt.Rows[i];
                    foreach (DataColumn dc in dt.Columns)
                    {
                        PropertyInfo pi = type.GetProperty(dc.ColumnName);
                        if (pi == null)
                        {
                            continue;
                        }
                        if (dr[dc.ColumnName] != DBNull.Value)
                        {
                            pi.SetValue(t, dr[dc.ColumnName], null);
                        }
                    }
                    TList.Add(t);
                }
                return TList;
            }
  • 相关阅读:
    函数
    数组
    类的例题
    异常语句
    类的学习
    for的穷举、迭代
    for循环
    switch case
    反相器,扇入扇出
    T触发器,JK触发器的verilog实现
  • 原文地址:https://www.cnblogs.com/sherlock99/p/3680549.html
Copyright © 2011-2022 走看看