• C# DataTable 转JSON、List 转DataTable、DataTable转List


    1.DataTable To Json:

    public string DataTableToJsonWithStringBuilder(DataTable table)
            {
                var jsonString = new StringBuilder();
                if (table.Rows.Count > 0)
                {
                    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();
            }

    2.List To DataTable

    /// <summary>
            /// List转换成DataTable
            /// </summary>
            /// <param name="list"></param>
            /// <returns></returns>
            public static System.Data.DataTable ListToDataTable(IList list)
            {
                System.Data.DataTable result = new System.Data.DataTable();
                if (list.Count > 0)
                {
                    PropertyInfo[] propertys = list[0].GetType().GetProperties();
                    foreach (PropertyInfo pi in propertys)
                    {
                        //获取类型
                        Type colType = pi.PropertyType;
                        //当类型为Nullable<>时
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        result.Columns.Add(pi.Name, colType);
                    }
                    for (int i = 0; i < list.Count; i++)
                    {
                        ArrayList tempList = new ArrayList();
                        foreach (PropertyInfo pi in propertys)
                        {
                            object obj = pi.GetValue(list[i], null);
                            tempList.Add(obj);
                        }
                        object[] array = tempList.ToArray();
                        result.LoadDataRow(array, true);
                    }
                }
                return result;
            }

     3.DataTable To List

    public static List<T> DataTableToList<T>(DataTable table) where T : class, new()
            {
                // 定义集合 
                List<T> ts = new List<T>();
                if (table.Rows.Count>0)
                {
                    //定义一个临时变量 
                    string tempName = string.Empty;
                    //遍历DataTable中所有的数据行 
                    foreach (DataRow dr in table.Rows)
                    {
                        T t = new T();
                        // 获得此模型的公共属性 
                        PropertyInfo[] propertys = t.GetType().GetProperties();
                        //遍历该对象的所有属性 
                        foreach (PropertyInfo pi in propertys)
                        {
                            tempName = pi.Name;//将属性名称赋值给临时变量 
                                               //检查DataTable是否包含此列(列名==对象的属性名)  
                            if (table.Columns.Contains(tempName))
                            {
                                //取值 
                                object value = dr[tempName];
                                //如果非空,则赋给对象的属性 
                                if (value != DBNull.Value)
                                {
                                    pi.SetValue(t, value, null);
                                }
                            }
                        }
                        //对象添加到泛型集合中 
                        ts.Add(t);
                    }
                }
                return ts;
            }
  • 相关阅读:
    桌面图标背景透明
    如何做好一个中小型企业计算机网络管理员
    打开IE8总是提示欢迎使用?怎样使它不提示?
    js 操作select和option,添加select列
    bios 被加密,怎么进入bios
    Foxmail自动收取新邮件
    代码片段
    提高生活幸福感的13个方法
    水晶报表的真实体验
    游标替代
  • 原文地址:https://www.cnblogs.com/guozhaoxin/p/14154889.html
Copyright © 2020-2023  润新知