LINQ to SQL SubmitChangess() progress

2019-02-23 17:21发布

问题:

I'm using LINQ to SQLto import old DBF files into MSSQL. I'm reading all rows and adding them to database using ctx.MyTable.InsertOnSubmit(row)

After reading phase is completed I have around 100 000 pending inserts. ctx.SubmitChanges() naturally is taking a long time.

Is there any way to track progress of the ctx.submitchanges()? Can ctx.Log somehow be used for this purpose?

Update: Is it possible to use ctx.GetChangeSet().Inserts.Count and track insert statements using the Log?

Dividing ctx.SubmitChanges() into smaller chunks is not working for me, because I need transaction, all or nothing.

Update 2: I've found nice class ActionTextWriter using which I will try to count inserts.

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

Update 3:

I've build first code prototype, it's not optimized. It seems to be working :)

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

回答1:

I've managed to get progress informations by parsing log and using 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++;
});