How to use SqlBulkCopy to prevent duplicates insert?

I am going to use SqlBulkCopy, but I need to prevent duplicates insert. What is the best way to do it? Have I do check existence for each item before make execute WriteToServer command?

--------------Solutions-------------

Bring all the values form the table into memory, you can than meake sure no already existing items are inserted using Except. Something like:

var existingValues = GetAllValuesFromDb();
var newValues = GetNewValues(); //Add .Distinct() to remove duplicates from the new values
var valuesToInsert = newValues.Except(existingValues);

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.

Category:c# Time:2012-03-29 Views:0

Related post

  • Rails: Prevent duplicate inserts due to pressing back button and save again 2011-01-11

    Think about a simple Rails scaffold application with a "new" action containing a form to add records to a database with a "save" button. After the "create" action the controller redirects to the "show" action, where the user can use the "edit" link t

  • Prevent duplicate insert without session 2011-01-31

    im working on sharepoint 2010 (using visual webpart) is there any way to prevent duplicate insert in a list when page is refreshed using F5, without using session thx --------------Solutions------------- The sharepoint "way" is usually to redirect to

  • Prevent duplicate inserts without using unique keys in Oracle 2011-11-08

    This is a use case in member enrollment via web application/web service. We have a complex algorithm for checking if a member is duplicate, by looking at multiple tables like phone,address etc. The algorithm varies based on member's country. So this

  • Preventing Duplicate Inserts Into SQL With PHP 2008-11-09

    I'm going to running thousands of queries into SQL and I need to prevent the duplication of field 'domain'. Never had to do this before and any help would be appreciated. --------------Solutions------------- You probably want to create a "UNIQUE" con

  • prevent duplicate insert by using updatepanel 2011-01-14

    i wrote a page which are several fields and there is gridview which keeps the data inserted bottom of the page and i added a updatepanel... when i click the insert double data inserting to grid how to prevent it... --------------Solutions------------

  • can isolation levels prevent duplicate insert? 2012-04-09

    i have a table like this UserPageId (Primary Key) int UserId (user Foreign Key) int PageId (page Foreign Key) int and the whole senario is to prevent user to add one page more than once,,according to each user can be use with several person and we mi

  • How to prevent duplicate records being inserted with SqlBulkCopy when there is no primary key 2010-04-07

    I receive a daily XML file that contains thousands of records, each being a business transaction that I need to store in an internal database for use in reporting and billing. I was under the impression that each day's file contained only unique reco

  • Preventing Duplicate Table Inserts 2011-06-17

    The database is SQL Server 2008. I have a query which extracts rows from one or more tables and then attempts to insert them into a table variable. I'd like an efficent way to prevent duplicate inserts, so what I came up with was: INSERT INTO @MyTabl

  • preventing duplicate table inserts using jdo in android connected appengine 2012-03-18

    I would like to know how to prevent duplicate inserts when doing an RPC call from an Android client connected to app engine. Below is my code and what I tried at the back-end but when I do this I get an "Internal Server Error". public void createenti

  • How to prevent duplicate rows in insert query using Oracle? 2009-10-31

    I have table A in Oracle that has a primary key (id). I need to insert data into this table. How do I prevent duplicate rows? --------------Solutions------------- If the id column is marked as the PK you will not be able to insert a duplicate key, th

  • Prevent duplicate record insert on manual page refresh 2010-09-16

    I have a problem which has only just surfaced itself. I'm working in a MVC environment. The method names in my interface class match those of the request module and action i.e. ?module=test&action=action would result in a method called public fun

  • How to prevent duplicate values from inserting mySQL considering two columns? 2011-10-20

    Here is my table: Table Name: UserLinks Link_ID User_1 User_2 1 234325 100982 2 116727 299011 3 399082 197983 4 664323 272351 Basically, in this table a duplicate value is: Link_ID User_1 User_2 1 232 109 2 109 232 I have looked around and found that

  • Insert Query (that prevents duplicates) 2014-09-07

    I saw a question similar to mine and I tried to modify the code but to no avail. I have a multiple-field index but it doesn't seem to be stopping duplicates from being imported or entered. I will post here the fields in the index as well as the code

  • Prevent Duplicate Entries and alert via a alert box if user insert duplicate entry 2010-07-09

    How i Prevent Duplicate Entries in database at the time of form submit and display an alert box and stop form submission if entry is duplicate. --------------Solutions------------- You can add a validates_uniqueness_of validation to your model: class

  • Adding a constraint to prevent duplicates in SQL Update Trigger 2009-02-13

    We have a user table, every user has an unique email and username. We try to do this within our code but we want to be sure users are never inserted (or updated) in the database with the same username of email. I've added a BEFORE INSERT Trigger whic

  • How can I constrain multiple columns to prevent duplicates, but ignore null values? 2009-03-23

    Here's a little experiment I ran in an Oracle database (10g). Aside from (Oracle's) implementation convenience, I can't figure out why some insertions are accepted and others rejected. create table sandbox(a number(10,0), b number(10,0)); create uniq

  • What's the best method to stop duplicate inserts into the database 2009-04-17

    I have a forum application using asp.net, c#, sql server and linq to sql. It suffers from multiple inserts, I think when the insert button is pressed but also maybe when the browser is refreshed or back is clicked. What's the most comprehensive way,

  • How to prevent duplicate entries in an Access database using c#? 2009-05-16

    i have a table of MS Access which contains one column and many rows. The value of a textbox saved to this table. I want to prevent duplicate entries from being submitted. For example, if I type "ankush" in the textbox and this entry already exists in

  • SQLite: prevent duplicates 2009-07-09

    I would like to create a table to store device settings. The table has three rows: id, parameter_name and parameter_value. The table was created by executing the following query statement: DATABASE_CREATE = "create table DATABASE_TABLE (KEY_ID INTEGE

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.425 (s). 13 q(s)