SQLBUlkCopy to call StoredProcedure to insert or update data in SQLDb

c# sqlbulkcopy sql-server

Question

I am having a list of records which I need to insert or update in a SQL DB based on whether the record is present or not present in the database.

The current flow is I process each record 1 by 1 and then call a Stored Procedure from my C# code which does the task of inserting or updating the database.

The above process is very inefficient, Can i use SQL Bulk Copy to insert these in once into the SQLDb .

Will above increase the performance .

Regards Ankur

Expert Answer

SqlBulkCopy can only insert. If you need to upsert, you might want to SqlBulkCopy into a staging table (a separate table off to one side that isn't part of the main model), and then do the merge in TSQL. You might also want to think about concurrency (how many people can be using the staging table at once, etc).



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why