Mayo
Mayo

Reputation: 10772

Use LINQ to insert data from dataset to SQL

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?

Upvotes: 1

Views: 1485

Answers (3)

roufamatic
roufamatic

Reputation: 18485

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.

Upvotes: 2

Andrew Lewis
Andrew Lewis

Reputation: 5256

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

Upvotes: 1

Rebecca Chernoff
Rebecca Chernoff

Reputation: 22607

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.

Upvotes: 2

Related Questions