zoukankan      html  css  js  c++  java
  • DataTable转换实体类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.OracleClient;
    using System.Configuration;
    using System.Text.RegularExpressions;
    using DataAccess;
    using System.Diagnostics;
    using System.Reflection;
    namespace Business
    {
    /// <summary>
    /// 实体类转换
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CModelTableFill<T> where T : new()
    {
    /// <summary>
    /// 填充多个实体类对象
    /// </summary>
    /// <param name="dt">数据集</param>
    /// <returns>实体类</returns>
    public List<T> FillModel(DataTable dt)
    {
    if (dt == null || dt.Rows.Count == 0)
    {
    return null;
    }
    T t = new T();
    List<T> modelList = new List<T>();
    PropertyInfo[] arrayInfo = t.GetType().GetProperties();

    foreach (DataRow itemRow in dt.Rows)
    {
    T model = new T();
    foreach (DataColumn itemColumn in dt.Columns)
    {
    foreach (PropertyInfo info in arrayInfo)
    {
    if (info.Name.ToUpper() == itemColumn.ColumnName)
    {
    if (itemRow[itemColumn.ColumnName] != DBNull.Value)
    {
    info.SetValue(model, itemRow[itemColumn.ColumnName].ToString(), null);
    }
    }
    }
    }
    modelList.Add(model);
    }
    return modelList;
    }
    /// <summary>
    /// 填充一个实体类对象
    /// </summary>
    /// <param name="dt">数据集</param>
    /// <returns>实体类</returns>
    public T FillModelRow(DataTable dt)
    {
    if (dt == null || dt.Rows.Count == 0)
    {
    return default(T);
    }
    T model = new T();
    PropertyInfo[] arrayInfo = model.GetType().GetProperties();
    for (int i = 0; i < dt.Columns.Count; i++)
    {
    foreach (PropertyInfo info in arrayInfo)
    {
    if (info.Name.ToUpper() == dt.Columns[i].ColumnName)
    {
    if (dt.Rows[0][i] != DBNull.Value)
    {
    info.SetValue(model, dt.Rows[0][i].ToString(), null);
    break;
    }
    }
    }
    }
    return model;
    }
    }
    }

  • 相关阅读:
    python基础
    ubuntu下使用mutt+msmtp发送邮件
    VC :在对话框中的控件中绘图
    VC :在对话框中绘图
    服务器性能/压力测试工具http_load、webbench、ab、Siege使用教程
    Xampp – Open SSL – Don’t know how to get public key from this private key
    apache 添加下载文件头
    nginx配置反向代理
    nginx的反向代理缓存
    varnishcache使用
  • 原文地址:https://www.cnblogs.com/changeMe/p/4421427.html
Copyright © 2011-2022 走看看