odiseh
odiseh

Reputation: 26517

Efficient way to insert all rows of a DataGridView into DB?

Is there any efficient way to insert all rows of a DataGridView into DB? using a foreach loop or using batch technic?

What is your option?

Thank you

Upvotes: 2

Views: 364

Answers (2)

abatishchev
abatishchev

Reputation: 100248

The most efficient solutions seems to be for me:

  1. Create a DataTable
  2. Fill it in a loop (it's always a loop to fill a DataTable/DataSet)
  3. Save it using SqlBulkCopy

Upvotes: 1

Tony Abrams
Tony Abrams

Reputation: 4673

I've done one of two things depending on the application needs.

1) You can save to the database per row, after an add/edit/delete has occured on a single row.

2) You can process them all at one time, and take the different types (add/edit/delete) using DataSet.GetChanges (because you should only process/save changes) and process each type differently (if needed). This is usually with a loop.

Additionally, If you have a TableAdapter. You can just use the Insert/Update commands as needed. They would batch process for you.

Upvotes: 0

Related Questions