简体   繁体   English

将CSV文件上传到SQL Server-身份问题

[英]Uploading a csv file to sql server - Identity problem

Given a column structure in a CSV file of: 给定CSV文件中的列结构:

First_Name, Last_Name, Date_Of_Birth

And a SQL Server table with a structure of 和一个具有以下结构的SQL Server表

ID(PK) | First_Name | Last_Name | Date_Of_Birth

(Field ID is an Identity with an auto-increment of 1) (字段ID是一个自动递增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? 我如何安排它,以便SQL Server不会尝试将csv文件中的First_Name列插入ID字段?

For info the csv is loaded into a DataTable and then copied to SQL Server using SqlBulkCopy 有关信息,将csv加载到DataTable中,然后使用SqlBulkCopy复制到SQL Server

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? 在导入之前,我应该修改csv文件添加ID列(目标表在导入之前被截断,因此无需担心重复的键值。)或者在数据表中添加ID列?

Or Is there a setting in Sql Server that i may have missed? 还是我可能错过的Sql Server中的设置?

You should add the ID column to the datatable, but do not worry about populating that field with data from the CSV. 您应该将ID列添加到数据表中,但不必担心会用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. 完成此操作后,数据应该会对齐,并且由于ID是具有自动增量的标识,因此可以将CSV加载到数据库中就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM