Abdelrahman Ahmed
Abdelrahman Ahmed

Reputation: 1

To transfer data from sql server database to another copy of the same database

I Have two copies of the same sql server database (DB1 & DB2). What is the best way to do the following :

Update the data of some tables in DB2 To be the same as DB1 (ie: DB1 is the source And DB2 is the destination) and update the data of some other tables in DB1 to be the same as DB2 (ie: DB2 is the source And DB1 is the destination)

How to make this using c# program? hope to help me in the best soulution to do that.

keep in mind that there is no connection between the program and the two copies at the same time and the tables has many relations up on Identity columns

Thanks :)

Upvotes: 0

Views: 1242

Answers (1)

Conrad Lotz
Conrad Lotz

Reputation: 8838

My suggestion would be to setup a linked server and then create stored procedure that make use of MERGE SQL command.

Note this is semi pseudo code example

 MERGE DB2 AS target
    USING DB1 AS source
    ON <Primary Keys>
    WHEN MATCHED THEN 
        UPDATE SET Key = source.Value
    WHEN NOT MATCHED THEN   
        INSERT (<Columns>)
        VALUES (source.values)
        OUTPUT deleted.*, $action, inserted.* INTO #MyTempTable;

Then you can create a SqlConnection/SqlCommand and then call the stored procedure. This code you can ultimatelt host in a windows service or normal .Net event i.e. button click, OnLoad as example.

Upvotes: 1

Related Questions