简体   繁体   中英

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 :)

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 ie button click, OnLoad as example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM