0

我正在使用 WCF 数据服务从我的 winforms 应用程序中的服务器获取数据。我正在尝试显示具有数百万条记录的庞大报告。尽管我正在明智地获取数据页面并将其存储到集合中,但内存不足。

这是用于查找总记录并为 List 分配内存的代码。

int totalRecords = ReportingService.Instance.CountRecords_ReportItemWiseSell(d1, d2);
List<Report_ItemWiseSellEntity> reportItems = new List<Report_ItemWiseSellEntity>(totalRecords);

这是收集所有分页数据的代码

int totalPageCount = (totalRecords / pageSize) + 1;
lvReport.Items.Clear();
for (int i = 1; i <= totalPageCount; i++){
   var tmpItems = new List<Report_ItemWiseSellEntity>();
   tmpItems = ReportingService.Instance.GetItemWiseSellReport(d1, d2, i, pageSize);
   reportItems.AddRange(tmpItems);

   ... //other stuff
   tmpItems = null;
   Application.DoEvents();
}

任何人都可以建议如何克服这个内存问题。还有其他选择吗?感谢您分享您的智慧和时间。

4

1 回答 1

0

除非 ... // other stuff包含对问题至关重要的内容,否则您逐页获取数据对 WinForms 端的内存消耗没有帮助,因为您Report_ItemWiseSellEntity将从服务中检索到的所有实例累积添加到reportItems列表中。

由于您的 UI 永远无法同时显示“数百万条记录”,因此您的策略应该是在 WinForms 应用程序的内存中仅保留当前随时显示/处理的那些记录。您将需要重新考虑将数据绑定到 UI 的设计,并将数据“页面”的检索与用户通过 UI 对报表的导航进行协调。

于 2012-09-05T12:41:22.887 回答