Slow Update/insert into SQL Server CE using LinqToDatasets

Posted by Vaccano on Stack Overflow See other posts from Stack Overflow or by Vaccano
Published on 2010-03-10T17:36:12Z Indexed on 2010/03/13 15:35 UTC
Read the original article Hit count: 784

I have a mobile app that is using LinqToDatasets to update/insert into a SQL Server CE 3.5 File.

My Code looks like this:

// All the MyClass Updates
MyTableAdapter myTableAdapter = new MyTableAdapter();

foreach (MyClassToInsert myClass in updates.MyClassChanges)
{
    // Update the row if it is already there
    int result = myTableAdapter.Update(myClass.FirstColumn, 
                                       myClass.SecondColumn, 
                                       myClass.FirstColumn);
    // If the row was not there then insert it.
    if (result == 0)
    {
        myTableAdapter.Insert(myClass.FirstColumn, myClass.SecondColumn);
    }
}

This code is used to keep the hand held database in sync with the server database. Problem is if it is a full update (first time for example) there are a lot of updates (about 125). That makes this code (and more loops like it take a very long time (I have three such loops that take over 30 seconds each).

Is there a faster or better way to do updates/inserts like this?

(I did see this Codeplex Project, but I could not see how to make it work with both updates and inserts.)

© Stack Overflow or respective owner

Related posts about linq-to-dataset

Related posts about LINQ