tags:

views:

109

answers:

3

Let's say I have a dataset in an ASP.NET website (.NET 3.5) with 5 tables, each has roughly 30,000 rows and an average of 12 columns. I want to insert all of the data from the dataset into 5 very-similar-but-not-quite-identical tables in SQL Server 2008. I also want to use LINQ (personal preference - trying to learn something new).

Is it as simple as iterating through the dataset and, for each row, creating a new instance of the associated class, initializing its data with the dataset's row, adding it to the data model, and then doing one giant SubmitChanges at the end?

Are there better ways of doing this with LINQ? Or is this the de-facto standard?

+2  A: 

Creating objects and inserting them is fine. But to avoid a gigantic commit at the end, you might want to perform a SubmitChanges() every 100 rows or so.

Alternately you could get a copy of Red Gate's "SQL Data Compare" utility if you have the cash. Then you never have to write one of these things again. :-)

Edit 2010-04-19: If you want to use a transaction, I think you should still use my approach instead of a single SubmitChanges(). In this case you'll want to explicitly manage your own transaction in L2S (see http://msdn.microsoft.com/en-us/library/bb386995.aspx). Run your queries in a try/catch and roll back the transaction if you get any failures.

Two last bits of advice:

  1. Make sure your ASP.NET timeout is set high enough.
  2. Consider printing out some kind of progress indicator. It makes running these kind of long-running things much more palatable.
roufamatic
Assuming I want everything bundled into a transaction, would it be more appropriate to do commits every 100 records or a single huge commit at the end? Or have I already passed the "reasonable solution" line by importing this much data through a Web app? :)
Mayo
Too much to say in a comment; editing the answer instead.
roufamatic
+2  A: 

Linq To Sql doesn't natively have anything like the SqlBulkCopy class. I did a quick search and it looks like there's an implementation for Linq To Sql. No clue if it is any good but it can't hurt to check it out.

rchern
+1  A: 

DataContext.ExecuteCommand can be used with an arbitrary SQL statement. You could do a "INSERT FROM".

Andrew Lewis
Interesting idea - I will try to spend some time with this today.
Mayo