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 va开发者_高级运维lues.) Or perhaps adding an id column to the Datatable?
Or Is there a setting in Sql Server that i may have missed?
Use SqlBulkCopy.ColumnMappings
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopycolumnmapping.aspx
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.
精彩评论