Posts Tagged ‘submitchanges’

Linq to sql, ctx.submitchanges progress

January 2, 2011 1 comment

Linq to sql provides no default mechanism for tracking progress of the ctx.submitchanges() method.

This can be troublesome if you are using batch import with linq to sql (which is not the best practice by itself).

I asked question on the stackoverflow

Response to the similar question was to divide inserts into multiple ctx.submitchanges() methods. This would work fine, but I needed transactions (all inserts or none)

I can get total number of inserts by using ctx.GetChangeSet().Inserts.Count;

Using ActionTextWriter class I found on

I was able to track progress of the inserts using following snippet of code

ctx.Log = new ActionTextWriter(s => {
  if (s.StartsWith("INSERT INTO"))

Linq to sql will write to the log file every sql statement. if the statements begins with INSERT INTO, that’s new insert.

Evertything I need for the progress bar is here 🙂

Also, performance wise, I’m reporting progress for every 10 000 inserted records. Refreshing screen too often can be slow sometimes.