dataTable转成对象、json、list

datatable转换成list集合

csharp 复制代码
public static T TableToEntity<T>(DataTable dt, int rowindex = 0, bool isStoreDB = true)
        {
            Type type = typeof(T);
            T entity = Activator.CreateInstance<T>();
            if (dt == null)
            {
                return entity;
            }
            DataRow row = dt.Rows[rowindex];
            PropertyInfo[] pArray = type.GetProperties();
            foreach (PropertyInfo p in pArray)
            {
                if (!dt.Columns.Contains(p.Name) || row[p.Name] == null || row[p.Name] == DBNull.Value)
                {
                    continue;
                }
                if (isStoreDB && p.PropertyType == typeof(DateTime) && Convert.ToDateTime(row[p.Name]) < Convert.ToDateTime("1753-01-02"))
                {
                    continue;
                }
                try
                {
                    var obj = Convert.ChangeType(row[p.Name], p.PropertyType);
                    p.SetValue(entity, obj, null);
                }
                catch (Exception)
                {

                }
            }
            return entity;
        }

//多行datatable数据转换为对象:

csharp 复制代码
 public static List<T> TableToList<T>(DataTable dt)
    {
        List<T> listT = new List<T>();

        Type type = typeof(T);

        if (dt == null)
        {
            return listT;
        }
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T entity = Activator.CreateInstance<T>();

                DataRow row = dt.Rows[i];

                PropertyInfo[] pArray = type.GetProperties();

                foreach (PropertyInfo p in pArray)
                {
                    if (!dt.Columns.Contains(p.Name) || row[p.Name] == null || row[p.Name] == DBNull.Value)
                    {
                        continue;
                    }

                    if (p.PropertyType == typeof(DateTime) && Convert.ToDateTime(row[p.Name]) < Convert.ToDateTime("1753-01-02"))
                    {
                        continue;
                    }
                    try
                    {
                        var obj = Convert.ChangeType(row[p.Name], p.PropertyType);
                        p.SetValue(entity, obj, null);
                    }
                    catch (Exception)
                    {
                        // throw;
                    }
                }
                listT.Add(entity);

            }
        }
        return listT;
    }

datatable转换成json

csharp 复制代码
public static string DataTableToJson(DataTable table)
        {
            var JsonString = new StringBuilder();
            if (table==null)
            {
                return "数据内容为null";
            }
            if (table.Rows.Count > 0 && table!=null)
            {
                JsonString.Append("[");
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    JsonString.Append("{");
                    for (int j = 0; j < table.Columns.Count; j++)
                    {
                        if (j < table.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == table.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
                        }
                    }
                    if (i == table.Rows.Count - 1)
                    {
                        JsonString.Append("}");
                    }
                    else
                    {
                        JsonString.Append("},");
                    }
                }
                JsonString.Append("]");
            }
            return JsonString.ToString();
        }
相关推荐
武藤一雄5 小时前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore
淼淼爱喝水8 小时前
ESXi 给 Windows Server 2008 虚拟机添加磁盘教程
windows·esxi·虚拟机
勤自省8 小时前
《RDK X5 ROS 2 Humble 安装与验证:从零到 Hello World》
windows·ubuntu·ssh·ros2
docsz8 小时前
Windows开发环境配置
windows
eggwyw9 小时前
PHP搭建开发环境(Windows系统)
开发语言·windows·php
运维行者_12 小时前
通过OpManager的Windows服务监控能力释放最佳IT网络性能
服务器·开发语言·网络·windows·web安全·php
oscar99912 小时前
Windows下快速安装OpenCode及使用—PowerShell+Chocolatey
windows·opencode
元Y亨H12 小时前
Python 获取 Windows 设备信息笔记
windows·python
xiaoshuaishuai812 小时前
C# Submodule 避坑指南
服务器·数据库·windows·c#
sagima_sdu13 小时前
Codex 使用指南(技术向):App、CLI 与工作流接入
linux·运维·语言模型·json