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();
        }
相关推荐
杨凯凡1 小时前
Docker环境搭建:Windows/macOS/Linux全平台教程
windows·macos·docker
阿昭L1 小时前
COM组件
windows
歪歪1006 小时前
解决多 Linux 客户端向 Windows 服务端的文件上传、持久化与生命周期管理问题
linux·运维·服务器·开发语言·前端·数据库·windows
山川而川-R7 小时前
ubuntu摄像头型号匹配不上_11-6
linux·windows·ubuntu
一般社员11 小时前
Windows导入大型sql文件到mysql
windows·sql·mysql
视觉AI12 小时前
HTTP 请求与数据交互全景指南:Request、GET、POST、JSON 及 curl
http·json·交互
A尘埃16 小时前
项目三:信息抽取与图谱问答(医疗科研文献知识图谱与智能问答平台)
人工智能·windows·知识图谱
winkel_wang19 小时前
think-cell 无法与 WPS Office 搭配使用
windows·wps·think-cell
Aurora(^*_*^)1 天前
Neo4j Windows桌面版安装及更改默认数据存储位置
windows·neo4j
0x00071 天前
翻译《The Old New Thing》- 为什么 SHFormatDateTime 要接收一个未对齐的 FILETIME?
c++·windows