How to use SqlBulkCopy to prevent duplicates insert?

c# sqlbulkcopy sql-server-2008

Question

I am going to use SqlBulkCopy, but I need to prevent duplicates insert. What is the best way to do it? Have I do check existence for each item before make execute WriteToServer command?

Popular Answer

The best way to do this would be to add a unique index onto the table you are inserting the data into. This will prevent any duplicate entries from being added to the table.

This also prevents you from having to potentially load too much data into memory and creating other problems for yourself.



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