[英]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). 我有同一SQL Server数据库的两个副本(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) 更新DB2中某些表的数据,使其与DB1相同(即:DB1是源,DB2是目标),并将DB1中其他表的数据更新为与DB2相同(即:DB2是源并且DB1是目的地)
How to make this using c# program? 如何使用C#程序做到这一点? 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 请记住,程序与两个副本同时没有连接,并且表在Identity列上有很多关系
Thanks :) 谢谢 :)
My suggestion would be to setup a linked server and then create stored procedure that make use of MERGE
SQL command. 我的建议是设置一个链接服务器,然后创建使用MERGE
SQL命令的存储过程。
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. 然后,您可以创建一个SqlConnection/SqlCommand
,然后调用存储过程。 This code you can ultimatelt host in a windows service or normal .Net event ie button click, OnLoad as example. 您可以将此代码最终托管在Windows服务或正常的.Net事件中,例如,单击按钮,例如OnLoad。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.