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
The most efficient solutions seems to be for me:
- Create a
DataTable
- Fill it in a loop (it's always a loop to fill a DataTable/DataSet)
- Save it using
SqlBulkCopy
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.
精彩评论