开发者

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:

  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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜