使用MVC从后台取数据,当数据量大的时候出现,字符串的长度超过了maxJsonLength属性设置值。

 

 

 

1.测试数据量小的时候是没有问题的。当数据量大的时候会出现问题。

这是数据量小的时候的代码,

        public JsonResult GetMultiGND(MultiGNDModels mgm)
        {
            try
            {
 
                DataTable dt = com.canmax.DAL.DataAnalyse.GetMultiGND(mgm);
                string js = JsonConvert.SerializeObject(dt); 

                return Json(js);
                 
                return Json(dt);

            }
            catch (Exception ee)
            {
                var resultmodel = new ResultModel();
                resultmodel.ErrorCode = "1";
                resultmodel.Message = ee.Message;
                return Json(resultmodel);
            }

        } 
  

使用MVC从后台取数据,当数据量大的时候出现,字符串的长度超过了maxJsonLength属性设置值。_第1张图片

 数据量大的话,需要用下面的代码

        public JsonResult GetMultiGND(MultiGNDModels mgm)
        {
            try
            {
 
                DataTable dt = com.canmax.DAL.DataAnalyse.GetMultiGND(mgm);

                List ls = 
                   DataTableExtend.ToDataList(dt);
 
                var jsonResult = Json(ls, JsonRequestBehavior.AllowGet);
                jsonResult.MaxJsonLength = Int32.MaxValue;
                return jsonResult;

            }
            catch (Exception ee)
            {
                var resultmodel = new ResultModel();
                resultmodel.ErrorCode = "1";
                resultmodel.Message = ee.Message;
                return Json(resultmodel);
            }

        } 
  

 我的程序还会初选循环引用的错误,则需要把datatable转换为List即可。

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace com.canmax.Extend
{
    /// 
    /// DataTable扩展方法类
    /// 
    public static class DataTableExtend
    {
        /// 
        /// DataTable转成List
        /// 
        public static List ToDataList(this DataTable dt)
        {
            var list = new List();
            var plist = new List(typeof(T).GetProperties());

            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }

            foreach (DataRow item in dt.Rows)
            {
                T s = Activator.CreateInstance();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
                    if (info != null)
                    {
                        try
                        {
                            if (!Convert.IsDBNull(item[i]))
                            {
                                object v = null;
                                if (info.PropertyType.ToString().Contains("System.Nullable"))
                                {
                                    v = Convert.ChangeType(item[i], Nullable.GetUnderlyingType(info.PropertyType));
                                }
                                else
                                {
                                    v = Convert.ChangeType(item[i], info.PropertyType);
                                }
                                info.SetValue(s, v, null);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("字段[" + info.Name + "]转换出错," + ex.Message);
                        }
                    }
                }
                list.Add(s);
            }
            return list;
        }

        /// 
        /// DataTable转成实体对象
        /// 
        /// 
        /// 
        /// 
        public static T ToDataEntity(this DataTable dt)
        {
            T s = Activator.CreateInstance();
            if (dt == null || dt.Rows.Count == 0)
            {
                return default(T);
            }
            var plist = new List(typeof(T).GetProperties());
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
                if (info != null)
                {
                    try
                    {
                        if (!Convert.IsDBNull(dt.Rows[0][i]))
                        {
                            object v = null;
                            if (info.PropertyType.ToString().Contains("System.Nullable"))
                            {
                                v = Convert.ChangeType(dt.Rows[0][i], Nullable.GetUnderlyingType(info.PropertyType));
                            }
                            else
                            {
                                v = Convert.ChangeType(dt.Rows[0][i], info.PropertyType);
                            }
                            info.SetValue(s, v, null);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("字段[" + info.Name + "]转换出错," + ex.Message);
                    }
                }
            }
            return s;
        }

        /// 
        /// List转成DataTable
        /// 
        /// 实体类型
        /// 实体集合
        public static DataTable ToDataTable(List entities)
        {
            if (entities == null || entities.Count == 0)
            {
                return null;
            }

            var result = CreateTable();
            FillData(result, entities);
            return result;
        }

        /// 
        /// 创建表
        /// 
        private static DataTable CreateTable()
        {
            var result = new DataTable();
            var type = typeof(T);
            foreach (var property in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
            {
                var propertyType = property.PropertyType;
                if ((propertyType.IsGenericType) && (propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    propertyType = propertyType.GetGenericArguments()[0];
                result.Columns.Add(property.Name, propertyType);
            }
            return result;
        }

        /// 
        /// 填充数据
        /// 
        private static void FillData(DataTable dt, IEnumerable entities)
        {
            foreach (var entity in entities)
            {
                dt.Rows.Add(CreateRow(dt, entity));
            }
        }

        /// 
        /// 创建行
        /// 
        private static DataRow CreateRow(DataTable dt, T entity)
        {
            DataRow row = dt.NewRow();
            var type = typeof(T);
            foreach (var property in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
            {
                row[property.Name] = property.GetValue(entity) ?? DBNull.Value;
            }
            return row;
        }
    }
}
 

 

你可能感兴趣的:(MVC4)