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;
            }
  • 相关阅读:
    解决:Android 8.0检测不到当前的activity
    flask学习(十三):过滤器
    打开相册上传图片
    完整的项目
    解决ScrollView滑动RecyclerView的卡顿
    RxJava
    CoordinatorLayout
    NestedScrollView,RecyclerView
    ViewPageIndicator
    RxJava的实现原理
  • 原文地址:https://www.cnblogs.com/dongteng/p/9139202.html
Copyright © 2011-2022 走看看