• c# DataTable 数据集处理DataTableHandler


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Collections;
    using System.IO;
    using System.Xml.Serialization;
    
    namespace HuaTong.General.Utility
    {
        /// <summary>
        /// 数据集处理
        /// </summary>
        public static class DataTableHandler
        {
            /// <summary>  
            /// 检查DataSet是否可用
            /// </summary>  
            public static bool CheckData(this DataSet ds, int valNum)
            {
                var flag = ds != null && ds.Tables.Count > 0;
                if (flag)
                {
                    int count = 0;
                    for (var i = 0; i < valNum; i++)
                    {
                        var dtflag = ds.Tables[i].CheckData();
                        if (dtflag) count++;
                    }
                    return count == valNum;
                }
                return flag;
            }
    
            /// <summary>
            /// 检查DataTable是否可用
            /// </summary>
            public static bool CheckData(this DataTable dt)
            {
                return dt != null && dt.Rows.Count > 0;
            }
    
            /// <summary>  
            /// 检查DataSet是否可用
            /// </summary>  
            public static DataRow FirstRow(this DataSet ds)
            {
                return ds.Tables[0].FirstRow();
            }
    
            /// <summary>
            /// 检查DataTable是否可用
            /// </summary>
            public static DataRow FirstRow(this DataTable dt)
            {
                return dt.Rows[0];
            }
    
            /// <summary>
            /// DataTable转换为List
            /// </summary>
            public static List<T> ToList<T>(this DataTable dt)
                where T : new()
            {
    
                List<T> list = new List<T>();
    
                foreach (DataRow dr in dt.Rows)
                {
                    T t = dr.ToEntity<T>();
                    //对象添加到泛型集合中
                    list.Add(t);
                }
                return list;
            }
    
            /// <summary>
            /// DataRow转换为实体
            /// </summary>
            public static T ToEntity<T>(this DataRow dr)
                where T : new()
            {
                if (dr == null)
                {
                    return default(T);
                }
    
                T model = new T();
                var props = ModelHandler.PropCache(model);
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    if (props.Any(m => StringHelper.IsEqualString(m.Name, dr.Table.Columns[i].ColumnName)))
                    {
                        var prop = props.Single(m => StringHelper.IsEqualString(m.Name, dr.Table.Columns[i].ColumnName));
                        if (dr[i] != null && dr[i] != DBNull.Value)
                        {
                            prop.SetValue(model, Convert.ChangeType(dr[i], prop.PropertyType), null);
                        }
                    }
                }
                return model;
            }
    
            /// <summary>
            /// List转换为DataTable
            /// </summary>
            public static DataTable ToDataTable<T>(this List<T> items)
            {
                var tb = new DataTable(typeof(T).Name);
    
                PropertyInfo[] props = ModelHandler.PropCache<T>();
    
                foreach (PropertyInfo prop in props)
                {
                    Type t = ModelHandler.GetCoreType(prop.PropertyType);
                    tb.Columns.Add(prop.Name, t);
                }
    
                foreach (T item in items)
                {
                    var values = new object[props.Length];
    
                    for (int i = 0; i < props.Length; i++)
                    {
                        values[i] = props[i].GetValue(item, null);
                    }
    
                    tb.Rows.Add(values);
                }
    
                return tb;
            }
    
            /// <summary>
            /// DataTable转换为XML
            /// </summary>
            public static string ToXML(this DataTable dt)
            {
                if (!string.IsNullOrEmpty(dt.TableName))
                {
                    dt.TableName = "datatable_1";
                }
                //xml序列化对象
                XmlSerializer xmlSerial = new XmlSerializer(typeof(DataTable));
                StringWriter sw = new StringWriter();
                xmlSerial.Serialize(sw, dt); // 序列化table
                StringReader sr = new StringReader(sw.ToString());
                return sr.ReadToEnd();
            }
        }
    }
  • 相关阅读:
    Django -- 多对多建表增删改查
    Django -- 一对多建表增删改查
    Django -- 图书管理系统
    Django -- 基础
    前端 -- jQuery
    前端 -- JavaScript-BOM
    前端 -- JavaScript-DOM
    前端 -- JavaScript基础
    前端 -- HTML块级标签
    linux目录结构
  • 原文地址:https://www.cnblogs.com/password1/p/5870689.html
Copyright © 2020-2023  润新知