C# - Does Datatable.merge() Add Duplicates Or Update?
Solution 1:
From the last paragraph from the Merge documentaton
When merging a new source DataTable into the target, any source rows with a DataRowState value of Unchanged, Modified, or Deleted, is matched to target rows with the same primary key values. Source rows with a DataRowState value of Added are matched to new target rows with the same primary key values as the new source rows.
It will update any row that has the same primary key. The primary key is set by the DataTable.PrimaryKey property
Solution 2:
The Remarks section if the Mergedocumentation contains the following paragraph
When performing a merge, changes made to the existing data before the merge are preserved by default during the merge operation. Developers can modify this behavior by calling one of the other two overloads for this method, and specifying a false value for the preserveChanges parameter.
However, the reference source shows
publicvoidMerge(DataTable table){
Merge(table, false, MissingSchemaAction.Add);
}
i.e. it actually calls the other overload with preserveChanges=false.
So looks like you should be getting the desired behavior. But if you want to be absolutely sure, just call explicitly the other overload and pass preserveChanges=false like this
datatable1.Merge(datatable2, false);
Post a Comment for "C# - Does Datatable.merge() Add Duplicates Or Update?"