admin管理员组文章数量:1287507
I'm running a C# Winforms application that each month uploads data from a .csv
file to a SQL Server database using SqlBulkCopy
, but I'm hitting multiple instances of the error:
Violation of PRIMARY KEY constraint 'MainTbl$ID'. Cannot insert duplicate key in object 'dbo.MainTbl'
I've searched the various instances of this error and haven't found one that seems to be the same. My data does not have the PK value included as the server is creating this automatically yet it still seems to be trying to use existing PK values. The data is around 550,000 rows each month with 54 columns.
Here's my code:
private void RunSQLBulkCopy(DataTable table)
{
GlobalInfo.dbConn.Open();
using (SqlTransaction transaction = GlobalInfo.dbConn.BeginTransaction())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(GlobalInfo.dbConn, SqlBulkCopyOptions.Default, transaction))
{
bulkCopy.DestinationTableName = "MainTbl";
try
{
table.Columns.Cast<DataColumn>().ToList().ForEach(x =>
bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(x.ColumnName, x.ColumnName)));
bulkCopy.BatchSize = 5000;
bulkCopy.WriteToServer(table);
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
transaction.Rollback();
UploadFailedTables.Add(table);
}
}
}
if (GlobalInfo.dbConn.State == ConnectionState.Open)
{
GlobalInfo.dbConn.Close();
}
}
In order to get round the issue for now I've set it up so I'm splitting my upload file into smaller tables then sending them into the upload process above. If the process hits the PK violation then the transaction rolls back and the table is stored in a list which is then re-processed later.
I can get all of the data into the database eventually but it requires multiple runs through , so the data itself seems to be OK but for some reason the auto created PK values seem to be using duplicates. The duplicates are from existing data and not data created during these uploads.
Any suggestion on where I'm going wrong or where else I should look?
From other questions I've read I've tried a few options e.g. from SqlBulkCopyOptions.KeepIdentity
to SqlBulkCopyOptions.Default
but no change.
I'm running a C# Winforms application that each month uploads data from a .csv
file to a SQL Server database using SqlBulkCopy
, but I'm hitting multiple instances of the error:
Violation of PRIMARY KEY constraint 'MainTbl$ID'. Cannot insert duplicate key in object 'dbo.MainTbl'
I've searched the various instances of this error and haven't found one that seems to be the same. My data does not have the PK value included as the server is creating this automatically yet it still seems to be trying to use existing PK values. The data is around 550,000 rows each month with 54 columns.
Here's my code:
private void RunSQLBulkCopy(DataTable table)
{
GlobalInfo.dbConn.Open();
using (SqlTransaction transaction = GlobalInfo.dbConn.BeginTransaction())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(GlobalInfo.dbConn, SqlBulkCopyOptions.Default, transaction))
{
bulkCopy.DestinationTableName = "MainTbl";
try
{
table.Columns.Cast<DataColumn>().ToList().ForEach(x =>
bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(x.ColumnName, x.ColumnName)));
bulkCopy.BatchSize = 5000;
bulkCopy.WriteToServer(table);
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
transaction.Rollback();
UploadFailedTables.Add(table);
}
}
}
if (GlobalInfo.dbConn.State == ConnectionState.Open)
{
GlobalInfo.dbConn.Close();
}
}
In order to get round the issue for now I've set it up so I'm splitting my upload file into smaller tables then sending them into the upload process above. If the process hits the PK violation then the transaction rolls back and the table is stored in a list which is then re-processed later.
I can get all of the data into the database eventually but it requires multiple runs through , so the data itself seems to be OK but for some reason the auto created PK values seem to be using duplicates. The duplicates are from existing data and not data created during these uploads.
Any suggestion on where I'm going wrong or where else I should look?
From other questions I've read I've tried a few options e.g. from SqlBulkCopyOptions.KeepIdentity
to SqlBulkCopyOptions.Default
but no change.
1 Answer
Reset to default 0After I ran EXEC sp_help 'dbo.mytable'
and noted the PK column error "No identity column defined" our DBA created a cloned table and defined the PK column as an identity column and now my bulk upload is working with the whole file uploading in one run.
本文标签: sql serverC SqlBulkCopyViolation of Primary Key constraintStack Overflow
版权声明:本文标题:sql server - C# SqlBulkCopy - Violation of Primary Key constraint - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268349a2368880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
EXEC sp_help 'dbo.mytable'
(change dbo to whatever schema your table is in). Several results will be returned, one of these results shows the identity column. Typically the seed is 0 or 1 and the increment is 1. – Alan Schofield Commented Feb 24 at 17:06