zoukankan      html  css  js  c++  java
  • C# 中List<T>与DataSet之间的转换

    C# 中List<T>与DataSet之间的转换

     

     public static DataSet ConvertToDataSet<T>(List<T> list)
            {
                if (list == null || list.Count <= 0)
                {
                    return null;
                }
    
                DataSet ds = new DataSet();
                DataTable dt = new DataTable(typeof(T).Name);
                DataColumn column;
                DataRow row;
    
                System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
    
                foreach (T t in list)
                {
                    if (t == null)
                    {
                        continue;
                    }
    
                    row = dt.NewRow();
    
                    for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                    {
                        System.Reflection.PropertyInfo pi = myPropertyInfo[i];
    
                        string name = pi.Name;
    
                        if (dt.Columns[name] == null)
                        {
                            column = new DataColumn(name, pi.PropertyType);
                            dt.Columns.Add(column);
                        }
    
                        row[name] = pi.GetValue(t, null);
                    }
    
                    dt.Rows.Add(row);
                }
                ds.Tables.Add(dt);
                return ds;
            }
  • 相关阅读:
    如何在markdown隐藏代码块
    html基础
    驻留机制
    字典
    echarts简单使用
    selenium的基本操作
    Excel上传、下载、models 自定义字段、批量执行(可选)
    django之自定义标签(路径url反向解码)
    邮件自动生成发送用例报告
    前台获取后台保存的数据
  • 原文地址:https://www.cnblogs.com/dongteng/p/9139202.html
Copyright © 2011-2022 走看看