Alex Rivera | Logout

bulk insert with linq-to-sql

Asked 2012-02-10T00:08:00.990
18

I have a query that looks like this:

using (MyDC TheDC = new MyDC())
{
   foreach (MyObject TheObject in TheListOfMyObjects)
   {
      DBTable TheTable = new DBTable();

      TheTable.Prop1 = TheObject.Prop1;
      .....
      TheDC.DBTables.InsertOnSubmit(TheTable);

   }
   TheDC.SubmitChanges();
}

This query basically inserts a list into the database using linq-to-sql. Now I've read online that L2S does NOT support bulk operations. Does my query work by inserting each element at a time or all of them in one write?

Thanks for the clarification.

Edit
Report

1 Answer

4

It will generate a single insert statement for every record, but will send them all to the server in a single batch and run in a single transaction.

That is what the SubmitChanges() outside the loop does.

If you moved it inside, then every iteration through the loop would go off to the server for the INSERT and run in it's own transaction.

I don't believe there is any way to fire off a SQL BULK INSERT.

answered 2012-02-10T01:25:36.917

Your Answer