What is the best way to deal with primary key violation errors when using SQLBulkCopy
Violation of PRIMARY KEY constraint 'email_k__'. Cannot insert duplicate key in object 'lntmuser.email'.
(i.e. if the row already exists in the destination table)?
Is there a way to skip inserting duplicate rows or will it need to be checked and processed before hand?
Here is the code I'm currently using:
var conPro = tx_ProConStr.Text; var conArc = tx_ArcConStr.Text; var con = new SqlConnection {ConnectionString = conPro}; var cmd = new SqlCommand("SELECT * FROM dbo.email", con); con.Open(); var rdr = cmd.ExecuteReader(); var sbc = new SqlBulkCopy(conArc) {DestinationTableName = "dbo.email"}; sbc.WriteToServer(rdr); sbc.Close(); rdr.Close(); con.Close();
source share