C# batch update to database using Stored Procedure

≯℡__Kan透↙ 提交于 2019-12-03 08:44:39

One thing I do a lot is send XML to SQL in one chunk. Performs much better than calling the DB several times in a loop.

Jon Galloway has and an oldie but a goodie here that you can reference.

All you need to do is convert your data or columns to XML. If you have this data in a class it's really simple. Check out this extension method - I don't remember where I found this code so I was not able to attribute it properly.

If you are using SQLServer 2008 or later, you can populate a DataTable, and pass it to the stored procedure as a table valued parameter.

Info on table valued parameters : http://www.sommarskog.se/arrays-in-sql-2008.html#TVP_in_TSQL

The stored proc can access the table parameter like a table variable, so you can perform the update in one chunk, which is good for performance.

This approach is similar to Billy Coover's XML approach, but using a DataTable instead of XML.

Seems to me that what you are doing should work fine. Alternatively you could build a 'batch' of updates (maybe 50, 100 or more updates each), and submit them all in chunks, but not sure that would help with performance at all, and would probably be less readable.

Are you having a performance problem? How many items are in the customCollection?

yes, build your sql query dynamically, yeah and I know it's bad performance compared to stored procedure, but considering all the connection open and close overhead you have in your code, it might not be a bad idea. I also notice that in your code you are trying to open and close the connection asap, it is good practice. However, I find that when I keep 1 connection opened for multiple update, it is ALOT faster compared to having open and close the connection multiple times. Scaleing might not be as good though because you are keeping a connection opened.

Get a DataTable from a TableAdapter (auto generated):

FooTableAdaptor tableAdapter = new FooTableAdapter();
FooDataTable dataTable = FooTableAdaptor.MyQuery(...);

Scroll through rows of DataTable making changes - this is just local.
Call

tableAdapter.Update(dataTable)  

to write the change in one hit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!