how Sql bulk copy works?

c# sqlbulkcopy

Question

I want to use SqlBulkCopy C# to insert rows to Sql DataBase but I don't understand if I define a table in the Db with X columns and I want to insert 2 DataTables- one of them contains only y/x columns and the second contains only z/x columns. when I use SqlBulkCopy, does it auto check the column name and insert value only if it is the appropriate column and null if not, or it always inserts values in the first columns and the last columns will be empty?

I tried to search it but I haven't find it? can anyone help?

Accepted Answer

Look for :SqlBulkCopyColumnMapping

When SqlBulkCopyColumnMapping is used, only columns for which mappings are created will be copied.

If you do not create a mapping for a column, it will be ignored by the copy process.


Popular Answer

Take a look at the SqlBulkCopy.ColumnMappings property. It allows you to map source and destination columns when the column count or positions do not match.



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