SQL Server: SqlBulkCopy import causes primary key violations

c# sqlbulkcopy sql-server

Question

I am trying to design a window based application in C#.NET. I am reading csv file in data grid view and then insert those data into database. I am using SqlBulkCopy to insert data from csv file into database. My concern is, when I am trying to insert data into database (which already consist data) I am getting error of primary key constraint. I want to know whether it is possible to compare value before inserting into database using SqlBulkCopy. If value exist in database it should do update.

Can anyone provide me logic for this.

Thanks.

Popular Answer

If you really, really know that the dupes aren't needed, just set the "Ignore Dupes" option on the index for the PK and you're done.



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