SqlBulkCopy 插入标识列
我正在使用 SqlBulkCopy 对象将几百万行生成的行插入数据库中。唯一的问题是我要插入的表有一个标识列。我尝试将 SqlBulkCopyOptions
设置为 SqlBulkCopyOptions.KeepIdentity
并将标识列设置为 0
's,DbNull.Value
和 null
。这些都没有奏效。我觉得我错过了一些非常简单的东西,如果有人能启发我那就太好了。谢谢!
编辑 澄清一下,我没有在导入的DataTable
中设置标识值。我希望它们作为导入的一部分生成。
编辑2 以下是我用来创建基本 SqlBulkCopy
对象的代码。
SqlBulkCopy sbc = GetBulkCopy(SqlBulkCopyOptions.KeepIdentity);
sbc.DestinationTableName = LOOKUP_TABLE;
private static SqlBulkCopy GetBulkCopy(SqlBulkCopyOptions options =
SqlBulkCopyOptions.Default)
{
Configuration cfg = WebConfigurationManager.OpenWebConfiguration("/RSWifi");
string connString =
cfg.ConnectionStrings.ConnectionStrings["WifiData"].ConnectionString;
return new SqlBulkCopy(connString, options);
}
I am using the SqlBulkCopy
object to insert a couple million generated rows into a database. The only problem is that the table I am inserting to has an identity column. I have tried setting the SqlBulkCopyOptions
to SqlBulkCopyOptions.KeepIdentity
and setting the identity column to 0
's, DbNull.Value
and null
. None of which have worked. I feel like I am missing something pretty simple, if someone could enlighten me that would be fantastic. Thanks!
edit To clarify, I do not have the identity values set in the DataTable
I am importing. I want them to be generated as part of the import.
edit 2
Here is the code I use to create the base SqlBulkCopy
object.
SqlBulkCopy sbc = GetBulkCopy(SqlBulkCopyOptions.KeepIdentity);
sbc.DestinationTableName = LOOKUP_TABLE;
private static SqlBulkCopy GetBulkCopy(SqlBulkCopyOptions options =
SqlBulkCopyOptions.Default)
{
Configuration cfg = WebConfigurationManager.OpenWebConfiguration("/RSWifi");
string connString =
cfg.ConnectionStrings.ConnectionStrings["WifiData"].ConnectionString;
return new SqlBulkCopy(connString, options);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
要让目标表分配标识,请勿使用
SqlBulkCopyOptions.KeepIdentity
选项。相反,不要从源映射身份,并且不要从源中提取它以发送到SqlBulkCopy
。To have the destination table assign the identity, DO NOT use the
SqlBulkCopyOptions.KeepIdentity
option. Instead, don't map the identity from the source, and don't extract it from source to send through toSqlBulkCopy
.填写
ColumnMapping
<BulkCopy
对象的 /a> 并且不映射标识列。标识列将由目标数据库生成。Fill the
ColumnMapping
of theBulkCopy
object and don't map the identity column. The identity column will be generated by the target database.您有两个选择 -
1 - 使用
KeepIdentity
并保留源的Identity
值。2 - 不要映射
Identity
字段。如果您不尝试分配值,目标表将自动分配一个值。You have two options -
1 - use
KeepIdentity
and preserve the source'sIdentity
values.2 - Don't map the
Identity
field. If you don't try to assign a value the target table will assign one automatically.这就是我在 .NET 中解决该问题的方法(
dt
是您的数据表):您基本上可以通过为目标列分配从以下位置开始的序数来跳过身份 (
Id
) 列1 而不是 0。This is how I solved it in .NET (
dt
is your data table):You basically skip the identity (
Id
) column by assigning your destination columns with an ordinal starting from 1 instead of 0.这是表格
下面的 C# 代码正在运行
This is the table
The bellow C# code is working
是的,您使用
SqlBulkCopyOptions.KeepIdentity
选项是正确的,然后批量复制编写器不会认为您的表结构是什么,该对象从开始列写入,因此根据我们的需要,我正在以相同的方式保留我的表中的标识字段只是您必须在数据表对象中使用其余所需的列创建一个额外的列,并将空值传递给该列,然后表自动处理标识。Yes, You are right using
SqlBulkCopyOptions.KeepIdentity
option then bulkcopy writer doesn't think that what is you table structure this object write from start column, so for our need, I am doing in same way to preserve identity field in my table just you have to make a extra column in you datatable object with rest of your needful columns and pass null values to this column then table automatically handles Identity.使用 JDBC SQLServerBulkCSVFileRecord 结构时,确实需要映射标识列,但标识列中的值将被忽略。
When using the JDBC SQLServerBulkCSVFileRecord structures, the identity column DOES need to be mapped, but the value in the identity column is ignored.
就我而言,结果是列名称内的空格,并且在其中一列中,我不小心在 SQL 表中使用了连字符 (-),而不是下划线 (_)。
我用下划线替换了sql表中的空格和连字符,它解决了问题。
In my case it turned out to be blank space inside the column name and in one of the columns I had accidently used hyphon (-) instead of underscore (_) in my SQL table.
I replaced blank space and hyphon with underscore in the sql table and it fixed the problem.
原因:- Excel 数据末尾有一些空行,可能看起来像空白行。批量上传试图将这些空白行上传到表中。
解决方案:- 仅选择包含数据的行 - 将数据复制到新工作表中。假设您的数据位于“表 1”中,请将其移至“表 2”并删除“表 1”。
Cause :- There were some empty rows in the excel at the end of the data, which possibly looks like blank rows. Bulk upload was trying to upload these blank rows into the table.
Solution :- Select only the rows which contains data - copy the data into the new sheet. Say you have your data in 'Sheet 1', move it to 'Sheet 2' and delete 'Sheet 1'.