4

我正在使用 LINQ to SQL 将旧的 DBF 文件导入 MSSQL。我正在读取所有行并将它们添加到数据库中ctx.MyTable.InsertOnSubmit(row)

阅读阶段完成后,我有大约 100 000 个待处理的插入。 ctx.SubmitChanges()自然是需要很长时间。

有什么方法可以跟踪进度ctx.submitchanges()吗?可以ctx.Log以某种方式用于此目的吗?

更新:是否可以ctx.GetChangeSet().Inserts.Count使用日志来使用和跟踪插入语句?

分成ctx.SubmitChanges()更小的块对我不起作用,因为我需要交易,要么全部要么全部。

更新 2: 我找到了不错的类 ActionTextWriter ,我将尝试使用它来计算插入次数。

http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers

更新 3:

我已经构建了第一个代码原型,它没有优化。它似乎正在工作:)

ctx.Log = new ActionTextWriter(s => {
 counter += s.Split(' ').Count(w => w.ToUpper() == "INSERT");
 ReportProgress(counter);
});
4

1 回答 1

0

我设法通过解析日志和使用 ActionTextWriter 来获取进度信息

http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers

ctx.Log = new ActionTextWriter(s => {
    if (s.StartsWith("INSERT INTO"))
        insertsCount++;
});
于 2010-12-30T20:06:36.260 回答