views:

265

answers:

2

Hello,

What is the best method for saving thousands of rows and after doing something, updating them.

Currently, I use a datatable, filling it, when done inserting by

MyDataAdapter.Update(MyDataTable)

After doing some change on MyDataTable, I again use MyDataAdapter.Update(MyDataTable) method.

Edit:

I am sorry for not providing more info.

There may be up to 200.000 rows which will be created from an XML file. There rows will be saved to the database. After than there will be some process for each row. And I will need to update each row in database.

Instead of updating row by row, I decided to update the datatable and using the same dataadapter to update the rows.

This is the best of me.

I think that there may be a smarter approach.

+1  A: 

If I understand correctly you are doing two separate operations: loading rows to a database, and then updating those rows.

If the rows you are inserting come from another ADO.NET supported datasource then you can use SqlBulkCopy to insert the rows in batches, which will be more efficient than using a datatable.

Once the rows are in the database I would assume you would be better off executing a SQLCommand to modify their values.

If you can provide more details about what--and why--you're asking the question then perhaps we can better tailor an answer for it.

STW
Actually the problem is updating them. I save it is ok. But i don't want to update them one by one. All of the rows inserted will need to be updated. And I really need to save them before updating them.
JCasso
+1  A: 

In Reacting to your comments:

An DataAdapter.Update() will Udate (and Insert/Delete) row by row. If you have individual changes there really is no faster way. If you have systematic changes, like SET Price = Price+ 2 WHERE SelByDate < '1/1/2010' you are better of by running a DbCommand against the database.

But maybe you should worry about transactions and error handling before performance.

Henk Holterman
@Henk Holterman: But i am using DataAdapter.UpdateBatchSize propery which increases perfonce.
JCasso
OK, setting that > 1 will update a batch of rows, less overhead. I don't think that changes the core of my answer.
Henk Holterman
@Henk Holterman, actually i do not have mass update. I send some columns of each row to a webservice and server sends me a unique value for each request, then i update. Anyway it seems like there is no better way of doing this.
JCasso