简体   繁体   English

如何合并和同步SQL Server数据库文件?

[英]How to merge and synchronize SQL Server Database Files?

I'm building a C# program for windows tablets that are synchronized with a database. 我正在为与数据库同步的Windows平板电脑构建一个C#程序。 They will each have their own local .MDF SQL Server database which they interact with using SQL Server Express. 他们每个人都有自己的本地.MDF SQL Server数据库,他们使用SQL Server Express进行交互。

However the users will take the tablets out of an internet connection and make changes. 但是,用户将从互联网连接中取出平板电脑并进行更改。 Then when one reconnects to the network which contains a "master copy" of the DataBase, I want to synchronize their .MDF database with that. 然后,当一个人重新连接到包含DataBase的“主副本”的网络时,我想要同步他们的.MDF数据库。 Then replace the database files of the computer and the tablet with the newly synchronized one. 然后用新同步的数据库替换计算机和平板电脑的数据库文件。

I have a uniqueidentifier column, and a datetime of when that row was last changed, so if there were conflicts I'd just take the most recent change. 我有一个uniqueidentifier列,以及该行最后一次更改的datetime时间,所以如果有冲突我会采取最近的更改。

I've read some literature on this, but I'd love to see an explicit example or tutorial of how to do it. 我已经阅读了一些关于此的文献,但我很想看到一个明确的例子或教程如何做到这一点。 I know what I want is Merge Replication , and that Microsoft Sync Framework seems to have the functionality I want. 我知道我想要的是合并复制 ,并且Microsoft Sync Framework似乎具有我想要的功能。 I just am struggling to implement it. 我正在努力实现它。 Alternatively, feel free to recommend a different tool to do it. 或者,您可以随意推荐其他工具。

Thanks in advance! 提前致谢!

I know this isn't much of an answer but I'm sure this is done with Microsoft Sync Framework . 我知道这不是一个答案,但我确信这是通过Microsoft Sync Framework完成的 I'm also quite sure you can easily sync a db from a tablet even though it was offline. 我也很确定你可以轻松地从平板电脑同步数据库,即使它是离线的。 The only limitation could be the lack of live sync for which you'd need to use Azure services, WebSockets, Apache MQ or whatever. 唯一的限制可能是缺少实时同步,您需要使用Azure服务,WebSockets,Apache MQ或其他任何东西。 Apologies for lack of more info, I'd post it as a comment but don't have enough points. 抱歉缺少更多信息,我会将其发布为评论,但没有足够的积分。

if it's a Windows Store App (WinRT), you have to use the Sync Framework Toolkit . 如果它是Windows应用商店应用(WinRT),则必须使用Sync Framework Toolkit

if plain Windows app (WPF, WinForms, etc...), yes, you can use Sync Framework. 如果是普通的Windows应用程序(WPF,WinForms等等),是的,您可以使用Sync Framework。

the idea for both is that your Windows app uses a local database (SQL CE, LocalDB, SQLite, etc...) for CRUD that you occasionally sync with a central server when you have connection (via WCF, like this ) 两者的想法是你的Windows应用程序使用本地数据库(SQL CE,LocalDB,SQLite等)进行CRUD,当你有连接时偶尔与中央服务器同步(通过WCF,像这样

MS Merge Replication is complex, but based on your use case you will not need most of the functionality. MS Merge Replication很复杂,但根据您的用例,您不需要大多数功能。 I will warn you ahead of time that it is known to have problems. 我会提前警告你,知道它有问题。 It's not very reliable in the Enterprise. 它在企业中不是很可靠。 It works but it just requires baby sitting. 它有效,但它只需要宝宝坐着。 If you don't mind the service calls, then proceed. 如果您不介意服务电话,请继续。 If you want a robust sync system (for your use case) then you are better off rolling your own using audit tables. 如果您需要一个强大的同步系统(针对您的用例),那么最好使用审计表来自行编辑。 It's not that difficult to do; 这并不困难; especially since you are already using guids as your primary keys. 特别是因为你已经使用guids作为主键。 You would also benefit from using sequential guids. 您还可以使用顺序guids。 Random guids will fragment your clustered index and your database performance will suffer greatly. 随机guid会破坏您的聚簇索引,您的数据库性能将受到很大影响。 If you need help with implementation, reach out to me. 如果您需要实施方面的帮助,请与我联系。 I've successfully done this for a client. 我已成功为客户完成此操作。 They process over a million records per day without a single issue. 他们每天处理超过一百万条记录,没有一个问题。 If you still want to use MS Merge Replication I can do that for you as well. 如果您仍想使用MS Merge Replication,我也可以为您执行此操作。

Most existing tools like Microsoft Sync and Merge-Replication ended seeming likebeing way too much overkill and being more hassle than they'd be worth. 像微软同步和合并复制这样的大多数现有工具看起来似乎太过于过分,而且比它们的价值更麻烦。

This is my SQL Script to attach the databases 这是我附加数据库的SQL脚本

CREATE DATABASE LocalDatabase  
ON (Filename = 'C:\ProgramData\Clayton\Database.mdf')
   , (Filename = 'C:\ProgramData\Clayton\Database_log.ldf') 
FOR ATTACH;
GO

EXEC sp_addlinkedserver @server='Server'

Then to sync the databases 然后同步数据库

-- update the client from the master 
MERGE [LocalDatabase].[dbo].[tableName] trgt
using [Server].[ServerDatabase].[dbo].[tableName] src

ON trgt.id = src.id 

WHEN matched AND trgt.lastmodified <= src.lastmodified THEN 
  -- if the master has a row newer than the client
  -- update the client                       
  UPDATE SET trgt.[allColumns]      = src.[allColumns],
             trgt.[id]              = src.[id], 
             trgt.[lastmodified]    = src.[lastmodified] 

-- delete any rows added by a client 
WHEN NOT matched BY source 
THEN 
  DELETE 

-- insert any rows added by the master 
WHEN NOT matched BY target 
THEN 
  INSERT ( [allColumns], 
           [id], 
           [lastmodified]) 
  VALUES (src. [allColumns], 
          src.[id], 
          src.[lastmodified]); 


-- now we update the master from the client
-- Note:
-- because the serverDB is a linked server 
-- we can't use another MERGE statement, otherwise
-- we get the error: "The target of a MERGE statement 
-- cannot be a remote table, a remote view, or a view over remote tables."

UPDATE
    serverDB

SET 
    [allColumns]        = [localDB].[allColumns],
    [id]                = [localDB].[id], 
    [lastmodified]      = [localDB].[lastmodified] 

FROM 
     [Server].[ServerDatabase].[dbo].[tableName] serverDB

INNER JOIN
     [LocalDatabase].[dbo].[tableName] localDB

-- update where the id is the same but the client is newer than the master

ON serverDB.id = localDB.id 
       AND localDB.lastmodified >= serverDB.lastmodified

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure SQL服务器如何同步数据库结构但不同步数据本身? - How to synchronize database structure but not synchronize data itself in Azure SQL Server? 如何将SQLite数据库与xamarin中的SQL服务器同步? - How to synchronize SQLite database with SQL Server in xamarin? 如何将 SQLite 数据库与服务器同步? - How to synchronize SQLite database with server? SQL Server在两个数据库之间同步数据 - SQL Server Synchronize data between two database SQL在本地和远程服务器之间同步数据库 - SQL synchronize database between local and remote server 如何在不使用复制的情况下将本地 SQL 服务器数据库的数据同步到远程 SQL 服务器? - How to synchronize data from local SQL Server database to remote SQL Server without using replication? 如何将MS SQL Server数据库合并到MySQL数据库? - How can I merge a MS SQL Server database to a MySQL database? 如何同步两个SQL Server 2005数据库? - How to synchronize two SQL Server 2005 databases? 如何持续同步两个 SQL Server 数据库? - How to continually synchronize two SQL Server databases? SQL Server和分布式数据库。 如何同步? - SQL Server and distributed databases. How synchronize?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM