将 DataSet 中的数千条记录更新到 SQL Server
我的数据集中有 50 万条记录,其中更新了 50,000 条。现在我需要将更新的记录提交回 SQL Server 2005 数据库。
考虑到此类更新可能很频繁(尽管并发不是问题,但性能才是问题),最好、最有效的方法是什么?
I have half a million records in a data set of which 50,000 are updated. Now I need to commit the updated records back to the SQL Server 2005 Database.
What is the best and efficient way to do this considering the fact that such updates could be frequent (though concurrency is not an issue but performance is)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用批量更新。
还记录在此处。
I would use a Batch Update.
Also documented here.
我同意大卫的回答,因为这就是我所使用的。然而,您可以采取另一种值得考虑的方法(毕竟所有情况都是不同的)——如果我有另一个类似的要求,我将来会考虑这种方法。
您可以将更新的记录批量插入到数据库中的新表中(使用 SqlBulkCopy),这是一种将数据加载到数据库的极快方式(示例)。然后在主表上运行 UPDATE 语句,从这个新表中提取更新后的值,最后删除该值。
使用 SqlDataAdapter 的批量更新方法允许您轻松处理特定行上的任何错误(例如,您可以告诉它在特定更新行发生错误时继续,这样它就不会停止整个过程)。
I agree with David's answer, as that's what I use. However, there is an alternative approach you could take which is worth considering (all situations are different after all) - it's something I would consider in the future if I had another similar requirement.
You could bulk insert the updated records into a new table in the DB (using SqlBulkCopy) which is an extremely fast way of loading data into the db (example). Then run an UPDATE statement on your main table to pull in the updated values from this new table which you would drop at the end.
The batched update approach of using SqlDataAdapter allows you to easily deal with any errors on specific rows (e.g. you could tell it to continue in the event of an error with a specific updated row so it doesn't stop the whole process).