Reputation: 26517
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
Reputation: 100248
The most efficient solutions seems to be for me:
DataTable
SqlBulkCopy
Upvotes: 1
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