【问题标题】:The wait operation timed out exception with LinqLinq 的等待操作超时异常
【发布时间】:2014-10-03 19:20:24
【问题描述】:

我需要开发简单的 Windows 桌面应用程序,它将 CSV 文件加载到单表 MDF 中,从中生成一些报告,然后删除数据。 CSV 文件可能包含数百万条记录...

当我尝试执行代码以在 dbGridView 中显示数据时,抛出异常“等待操作超时”。

  db dbEntities = new db();

    var ds = (from tbl in db.tbl_csv
              orderby f1
              select new
              {
                  f1= tbl.f1,
                  f2 = tbl.f2,
                  f3= tbl.f3,
                  f4= tbl.f4,
                  f5= tbl.f5,
                  f6= tbl.f6,
                  f7= tbl.f7,
                  f8= tbl.f8
              }
                  );
    var bds = ds.ToList();
    return Helpers.ToDataSet(bds);

这适用于 csv 包含少量数据,但当它有超过 70-80k 条记录时,会抛出异常...

有什么解决方法吗?

【问题讨论】:

  • 不要一次读取数百万条记录的数据。

标签: c# sql-server linq mdf


【解决方案1】:

一次读取n 条记录。例如

int totalRecords = ds.Count();
int n = 100;
int chunksRead = 0;
int recordsRead = 0;

while(recordsRead < totalRecords)
{
    ds.Skip(recordsRead).Take(n);
    // process n records
    ...
    chunksRead++;
    recordsRead = chunksRead * n;
}

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 2019-03-13
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    相关资源
    最近更新 更多