Home > Databases, Programming > Linq to sql, ctx.submitchanges progress

Linq to sql, ctx.submitchanges progress

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
http://stackoverflow.com/questions/4563828/linq-to-sql-submitchangess-progress

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
http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers

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

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

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.

About these ads
  1. JĂșlio Nobre
    September 12, 2012 at 1:06 pm

    Great article!

    Beside you performance suggestion, I would report progress every nth seconds instead of nth records. This would grant responsiveness along with performance.

    Cheers!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: