简体   繁体   English

如何使用SqlBulkCopy防止重复插入?

[英]How to use SqlBulkCopy to prevent duplicates insert?

I am going to use SqlBulkCopy, but I need to prevent duplicates insert. 我将使用SqlBulkCopy,但我需要防止重复插入。 What is the best way to do it? 最好的方法是什么? Have I do check existence for each item before make execute WriteToServer command? 在执行执行WriteToServer命令之前,我是否要检查每个项目的存在性?

The best way to do this would be to add a unique index onto the table you are inserting the data into. 最好的方法是在要向其中插入数据的表上添加唯一索引。 This will prevent any duplicate entries from being added to the table. 这样可以防止将任何重复的条目添加到表中。

This also prevents you from having to potentially load too much data into memory and creating other problems for yourself. 这也避免了您可能不得不将过多的数据加载到内存中并给自己造成其他问题。

you do not really specify what is your source. 您没有真正指定来源。 If it is a flat file you could SqlBulkCopy into a staging table and afterwards use a insert into sql statement that filters out the duplicates. 如果是平面文件,则可以将SqlBulkCopy放入登台表中,然后使用insert into SQL语句来过滤出重复项。

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

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