我需要开发简单的Windows桌面应用程序,它将在单表MDF中加载CSV文件,从中生成一些报告然后删除数据。 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的记录时,会抛出异常...
有没有解决方法呢?
答案 0 :(得分:2)
一次读取n
条记录数。 e.g。
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;
}