Reputation: 1051
I am now copying data from one client database DB2
to main database DB1
by using SqlBulkCopy
.
DataTable dtTable = new DataTable();
sqlDB2DataAdapter.Fill(dtTable); //select * from tblOrdersDB2
SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlDB1Connection)
bulkcopy.DestinationTableName = "dbo.tblOrdersDB1";
bulkcopy.WriteToServer(dtTable);
By creating INDEX ,when I will insert data with bulk to db, all duplicate values will not inserted.
The problem is I want to report a CSV file of all duplicate records being ignored.
How can I achieve this duplicate records?Is there a simple way to do that?
Thank you.
Upvotes: 1
Views: 2081
Reputation: 31
This may be a help
class TableComparer : EqualityComparer<DataRow>
{
public override bool Equals(DataRow c1, DataRow c2)
{
if (c1["field1"] == c1["field1"] &&
c1["field2"] == c1["field2"])
{
return true;
}
else
{
return false;
}
}
public override int GetHashCode(DataRow c)
{
int hash = 23;
hash = hash * 37 + c["field1"].GetHashCode();
hash = hash * 37 + c["field2"].GetHashCode();
return hash;
}
}
TableComparer eqc = new TableComparer();
var newValues = tempList.Rows.Cast<DataRow>().Distinct(eqc).ToList();
SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlDB1Connection)
bulkcopy.DestinationTableName = "dbo.tblOrdersDB1";
bulkcopy.WriteToServer(newValues);
Upvotes: 1
Reputation: 39085
Why don't you just filter our the duplicate records during the initial SELECT? And as a separate task select and report the duplicates the user. The duplicate thing really is a separate concern from the bulk copy.
Upvotes: 2