SqlBulkCopy to a table with a composite primary key

I am trying to use SqlBulkCopy to insert new rows into my DB table by manually populating a DataTable with my application.

This works great for all tables except a table with a composite primary key consisting of 3 columns . Whenever I try to execute SqlBulkCopy in this table, I get the following error:

Violation of PRIMARY KEY constraint 'PK_MYCOMPOSITEKEY'. Cannot insert duplicate key in object 'dbo.MyTable'.
The statement has been terminated.

Is it possible?

I tried setting up the primary keys of a DataTable with the following:

dt.PrimaryKey = new[] {dt.Columns["PKcolumn1"], dt.Columns["PKcolumn2"], dt.Columns["PKcolumn3"]};

but again, no luck.

+3
source share
3 answers

You have a data problem.

The input file has either or both of

, e pk,

pk

+1

. . , SQL. , , prod.

+1

, , ( , ). , .

However, the entire display of a DataSet or even DataReaders is an erratic exercise in comparisons, a poor impartial construct, a lot of unnecessary conversions, distributions, object-based values ​​[], and the whole thing becomes an order, type, and line-dependent mess (only MS could design and continue designing). On the other hand, OLEDB's internal interfaces are much cleaner.

+1
source

Source: https://habr.com/ru/post/1719759/


All Articles