I'm using SqlBulkCopy
class in C# to copy data from one SQL Server database to another in a fast way. The databases are in different servers and their datatables don't have any PK, so the process gets more complicated.
The problem is that the query I'm using to select data from the original database gets duplicate rows and SqlBulkCopy
cannot avoid insert duplicate records in destination database.
I cannot use SELECT *
because it throws an OutOfMemoryException
, so I do SELECT TOP X *
and load that data into a DataTable
. In each Datatable
I can remove the duplicate records using C#, but then when I select the next TOP X, the first row selected may be equal to the last one that was in the previous DataTable
and has been already inserted into the destination database. The DataTable
variable is always the same, it is reloaded!
I want to avoid duplicate records to be inserted without create PK because it's not applicable to my case. I really need to use SqlBulkCopy
because a fast copy is a system requirement. Any suggestion? Thank you in advance!