Uploading a csv file to sql server - Identity problem

.net c# csv sqlbulkcopy sql-server

Question

Given a column structure in a CSV file of:

First_Name, Last_Name, Date_Of_Birth

And a SQL Server table with a structure of

ID(PK) | First_Name | Last_Name | Date_Of_Birth

(Field ID is an Identity with an auto-increment of 1)

How do i arrange it so that SQL Server does not attempt to insert the First_Name column from the csv file into the ID field?

For info the csv is loaded into a DataTable and then copied to SQL Server using SqlBulkCopy

Should i be modifying the csv file before the import add the ID column (The destination table is truncated prior to import, so no need to worry about duplicate key values.) Or perhaps adding an id column to the Datatable?

Or Is there a setting in Sql Server that i may have missed?

Accepted Answer

You should add the ID column to the datatable, but do not worry about populating that field with data from the CSV.

Once that is done the data should line up fine and since ID is a Identity with Auto-Increment you should be fine with loading the CSV into the DB.





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