简体   繁体   English

如何设置SQL Server数据表插入和自动递增列

[英]How to set SQL Server datatable insert and autoincrement column

I am using a DataTable and SqlBulkCopy to insert data into a SQL Server database table. 我正在使用DataTableSqlBulkCopy将数据插入SQL Server数据库表中。

I have managed to bulk insert the rows using the following settings but only using negative values as otherwise I get conflicts as the value is not unique: 我设法使用以下设置批量插入行,但仅使用负值,否则由于值不唯一而导致冲突:

DataTable table = new DataTable();
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 0;
column.AutoIncrementStep = -1;
table.Columns.Add(column);
table.Columns.Add(DB_Base.DBTable_Start, typeof(DateTime));

I have a loop that generates rows to be inserted like this: 我有一个循环,它会生成要插入的行,如下所示:

table.Rows.Add(null,tsoOptions.start);

I then set the table and connection and write the data using the WriteToServer() method. 然后,我设置表和连接,并使用WriteToServer()方法写入数据。 This is all working fine and the rows appear but with negative autoincrement primary keys. 一切正常,出现行但带有负的自动增量主键。

How do I modify this so that it will append the rows with a positive value which continues after the last (MAX) value without reading the max value in a separate query? 我如何修改此值,以便它将在最后一个(MAX)值之后继续的正值附加行,而不在单独的查询中读取最大值?

Assuming your table in the database is created properly with the column auto-increment (ie IDENTITY ) turned on, don't duplicate this functionality in your code. 假设在数据库中的表是在打开列自动增加(即IDENTITY )的情况下正确创建的,请不要在代码中重复此功能。 Just send the records to the database with the null value for that column and the database will do its job. 只需将记录与该列的null值一起发送到数据库,数据库就会完成其工作。 Comment out these lines and try: 注释掉这些行,然后尝试:

//column.AutoIncrement = true;
//column.AutoIncrementSeed = 0;
//column.AutoIncrementStep = -1;

UPDATE Actually the best way to do it is by not mapping the identity column at all, so comment out all these lines: 更新实际上,最好的方法是根本不映射标识列,因此注释掉所有这些行:

//DataColumn column = new DataColumn();
//column.DataType = System.Type.GetType("System.Int32");
//column.AutoIncrement = true;
//column.AutoIncrementSeed = 0;
//column.AutoIncrementStep = -1;
//table.Columns.Add(column);
//table.Columns.Add(DB_Base.DBTable_Start, typeof(DateTime));

And make sure you're not using SqlBulkCopyOptions.KeepIdentity ( check this ). 并确保您没有使用SqlBulkCopyOptions.KeepIdentity选中此复选框 )。

Do you need to setup this in the DataTable object? 您是否需要在DataTable对象中进行设置? Usually I'd go on SSMS (Sql Server Management Studio) and set up the primary key as Identity and Increment to one. 通常,我会使用SSMS(Sql Server Management Studio)并将主键设置为Identity and Increment to one。 When I save in the database, I just don't send anything for this column: 当我保存在数据库中时,我只是不为该列发送任何内容:

1) Right-click on the table in SSMS tree on the left, choose design 1)右键单击左侧SSMS树中的表,选择设计 在此处输入图片说明

2) Click on your primary-key column and have a look on Column Properties section, you should set up like this: 2)单击您的主键列,然后在“列属性”部分中查看,您应该这样设置: 在此处输入图片说明

That should do the job! 那应该做的!

UPDATE: Like the other guy here suggested, comment out the lines of your code that you are trying to set up seed and identity and do this in SSMS instead. 更新:像这里建议的其他人一样,注释掉您试图设置种子和身份的代码行,并改为在SSMS中执行。

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

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