如何使用 SqlBulkCopy.WriteToServer 将行批量插入 SQL Server(仅当不存在时)
我们使用 SqlBulkCopy.WriteToServer 批量插入到 SQL Server,效果非常好。但是,当记录已存在时,它会失败。我们需要的是“忽略”那些已经存在的行,并插入不存在的行。
SqlException:违反主键约束“PK__Pharmacy__3214EC072C1E8537”。 无法在对象“dbo.Pharmacy”中插入重复的键。重复的键值是 (797cba76-8bbd-4dbd-a360-4f8e8a6ef85b)
我们如何使用 SqlBulkCopy.WriteToServer 插入行(如果它们不存在)而不破坏或失败。
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(dt);
}
catch (Exception ex)
{
throw new Exception($"BulkInject error in {dt.TableName}", ex);
}
更新: 值得一提的是,这在大多数情况下都运行良好,98% 的情况下都能正确批量插入。只有 2% 的情况下,某些行已经存在,这会导致批量插入失败。
我们需要什么:我们需要“忽略”这些行(如果存在)
我们要做的:将源数据库的数据传输到目标数据库。这不是完整的转移。我们传输源数据的子集。目标数据库不为空。它已经包含数据。所以更新不是一个选择。如果不存在,我们需要插入。
我们将大约 30 个表从源数据库批量插入到目标数据库。所以我们有一个通用函数来执行字段映射、批量插入等...它与处理所有这些表的函数相同。
再说一遍,我们需要什么:我们正在使用 SqlBulkCopy.WriteToServer,并且我们需要“忽略”行(如果存在)。谢谢
We are using SqlBulkCopy.WriteToServer to bulk insert to SQL Server and works very well. However, it fails when a record already exists. What we need is to "ignore" those rows that already exist, and insert the non-existing ones.
SqlException: Violation of PRIMARY KEY constraint 'PK__Pharmacy__3214EC072C1E8537'.
Cannot insert duplicate key in object 'dbo.Pharmacy'. The duplicate key value is (797cba76-8bbd-4dbd-a360-4f8e8a6ef85b)
How can we use SqlBulkCopy.WriteToServer to insert rows, if they do not exist, without breaking or failing.
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(dt);
}
catch (Exception ex)
{
throw new Exception(quot;BulkInject error in {dt.TableName}", ex);
}
Update:
It's important to mention that this works well most of the time, 98% of the time and bulk inserting properly. Just 2% of the time, some rows already exist that will cause the bulk insert to fail.
What we need: we need to "ignore" those rows if exist
What we do: Data transfer of source database to dest db. It's not a full transfer. We transfer a subset of the source data. The dest db is NOT empty. It already contains data. So update is NOT an option. We need to insert if not exists.
There are around 30 tables that we do bulk insert from source to dest db. So we have a generic function that does field mapping, bulk inserting, etc... it's the same function that handles all these tables.
Again, what we need: We are using SqlBulkCopy.WriteToServer and we need to "ignore" rows if they exist. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SqlBulkCopy顾名思义是用于复制(插入)批量记录,它不能执行更新操作。因此表值参数来救援,它允许我们使用 DataTable 将多条记录传递到存储过程,我们可以在存储过程中进行处理...
SQL Server 2008(或更高版本)提出了一个名为 MERGE 的好函数,它允许当记录不存在时执行 INSERT 操作,当表中存在记录时执行 UPDATE 操作。
您可以在 SQL Server 中创建用户定义的表类型
最后,创建以下类似类型的存储过程,它将接受整个 DataTable 作为参数,然后将表中不存在的和已经存在的所有记录插入到表中存在将被更新。
性能将不如批量复制实用程序,但不知何故,它是我在实时数据库处理报告数据库时使用的选项之一。
SqlBulkCopy as the name suggest is for copying (inserting) bulk records and it cannot perform update operation. Hence comes Table Valued Parameter to the rescue, which allows us to pass multiple records using a DataTable to a Stored Procedure where we can do the processing...
SQL Server 2008(or higher) came up with a nice function called MERGE, which allows to perform INSERT operation when records are not present and UPDATE when records are present in the table.
You can create a User Defined Table Type in SQL Server
Finally the following similar kind of stored procedure is created which will accept the whole DataTable as parameter and then will insert all records into the table that are not present in the table and the one that already exists will be updated.
The performance will be less as Bulk Copy Utlity but somehow it is one of option i have used while working on a reporting database from realtime DB.