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();
        }
相关推荐
jingshaoqi_ccc31 分钟前
windows 10系统下QT的安装及在Visual studio中的扩展安装
windows·qt·visual studio
ID_180079054735 小时前
淘宝 API 详情类 JSON 结构化解析实战(核心章节)
json
東雪木6 小时前
泛型、反射、注解(Spring 框架核心底层)专属复习笔记
java·windows·笔记·学习·spring
sun0077007 小时前
Windows下UniGetUI,Linux下敲命令
windows
流星白龙7 小时前
【MySQL高阶】18.缓冲池页管理
数据库·windows·mysql
AI行业学习9 小时前
PuTTY 工具下载部署、基础配置及 SSH 远程服务器连接完整操作指南Windows 平台 【2026.6.1】
运维·windows·ssh
tealcwu9 小时前
【Unity实战】Unity IAP 4.x 在 Windows Store (UWP) 平台上的实现指南
windows·unity·游戏引擎
爱讲故事的10 小时前
操作系统第四讲:OS Interfaces and Syscalls(操作系统接口与系统调用)
linux·windows·ubuntu
haven-85211 小时前
AI Agent 生态核心概念详解:Agent、MCP、Skill 与 JSON-RPC
人工智能·rpc·json
糖果店的幽灵11 小时前
LangChain 1.3 完全教程:从入门到精通-Part 10: Memory(记忆系统)
windows·microsoft·langchain