【问题标题】:System.OutOfMemoryException on gridview网格视图上的 System.OutOfMemoryException
【发布时间】:2014-01-14 14:47:38
【问题描述】:

我的申请有问题。

首先,对不起我的英语,我来自智利,我还在学习。

我有一种方法可以为 Excel 提供来自数据库的信息。客户不需要在屏幕上看到报告,他们只需要从应用程序下载。

此方法调用存储过程,它提供了太多数据(大约 600 列和 20000 行),因此,当将表分配给 GridView 时,在 DataBind() 上,会抛出 System.OutOfMemoryException

还有其他方法可以用数据加载 GridView 吗?我无法过滤或简化查询,因为客户需要报告中的每个日期。 我正在使用 Visual Studio 2010、Framework 4.0、SQL Server 2008。

这是方法:

private void getsReport()
{
    string reportName = "Globarl_Report_" +     User.Identity.Name + "_" + DateTime.Now.ToString("yyyyMMdd")+".xls";
    Database db = DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["SNAT"].ToString());
    DBCommandWrapper com = db.GetSqlStringCommandWrapper("usp_con_global_report);// sp that give report.

    com.CommandTimeout = 360;

    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    Page pagina = new Page();
    HtmlForm form = new HtmlForm();
    DataTable tabla = db.ExecuteDataSet(com).Tables[0]; ;
    GridView dg = new GridView();

    dg.EnableViewState = false;
    dg.DataSource = tabla;
    dg.DataBind();//THIS LINES THROWS EXCEPTION!!
    pagina.EnableEventValidation = false;
    pagina.DesignerInitialize();
    pagina.Controls.Add(form);
    form.Controls.Add(dg);
    pagina.RenderControl(htw);
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + nombreArchivo);
    Response.Charset = "UTF-8";
    Response.ContentEncoding = Encoding.Default;
    Response.Write(sb.ToString());
    Response.End();
}

感谢您的帮助!!

【问题讨论】:

  • 你有什么理由向用户展示 20.000 行吗?您应该添加搜索和过滤以及分页。还有600列???没有客户需要 20.000 行 600 列!
  • 是否可以修改usp_con_global_report 以限制您返回的行数?
  • 或在 google 上查找虚拟化
  • @ScottChamberlain,对,我的错 - msdn.microsoft.com/en-us/library/windows/apps/hh780657.aspx 这解释了虚拟化的概念(尽管它不适用于 asp.net)

标签: c# asp.net gridview


【解决方案1】:

您应该在 gridview 中添加一个寻呼机。

看这里:Paging and Sorting the GridView's Data

【讨论】:

    【解决方案2】:

    您是否将所有数据加载到 grig 以便仅导出到 Excel?

    在这种情况下,您可以在服务器上创建 csv 文件(按计划或通过客户端动态请求)并在页面上放置一个链接以下载它。

    以后可以用 Excel(或任何其他电子表格处理器)打开它

    【讨论】:

      猜你喜欢
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多