数据同步框架MS Sync Framework - IDE快速开发支持Local Database Cache

本文介绍如何利用MSSyncFramework快速搭建离线同步应用。通过Visual Studio提供的LocalDatabaseCache,结合SQLCompact与SQLServer2005/2008,实现客户端与服务器的数据同步。文中提供了具体步骤与示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据同步框架MS Sync Framework [术语、例子、参考资料、Tips]

http://www.cnblogs.com/2018/archive/2011/02/22/1961654.html 

数据同步框架MS Sync Framework-不同场景使用例子和简要分析

http://www.cnblogs.com/2018/archive/2011/02/23/1961657.html

以上两篇文章对框架的基础有了描述,通过IDE的提供Local Database Cache可以快速的实现一个常用的同步应用。具体如下:

VS 提供的基于MS Sync框架的支持,用于客户端离线的使用,客户端是SQL Compact,服务端可以是Sql Server 2005/2008

 

详细例子参考:

http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.Public/SyncTest.rar 

DbCache目录下是具体的例子,具体的建立步骤如下

模板

image

加入后会有*.sync文件,以后双击 这个可编辑和修改

同步设置

双击NorthWind.Server.sync 或NorthWind.Server.sync可以进行修改

image

SQL Server 2008是可以使用Sql Server change Tracking

SQLScripts SQLUndoScripts : 对数据库的Provison的支持和取消

NorthwindDataSet.xsd: 客户端的缓存访问接口

同步代码

            using (DataServiceProxy.NorthWindSyncContractClient DataSvc = new DataServiceProxy.NorthWindSyncContractClient())
            {                 ///nwCache.sdf把默认建表时的数据同步下来
                NorthwindDataSetTableAdapters.CustomersTableAdapter customersTableAdapter
                   = new NorthwindDataSetTableAdapters.CustomersTableAdapter();
                NorthwindDataSet northwindDataSet = new NorthwindDataSet();
                customersTableAdapter.Fill(northwindDataSet.Customers);

                new NorthwindDataSetTableAdapters.ProductsTableAdapter().Fill(northwindDataSet.Products);
                 ///修改(增删改)SQL 服务器的Customers表
                NorthWindSyncAgent syncAgent = new NorthWindSyncAgent(DataSvc);
                Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize();
                Debug.WriteLine(syncStats.TotalChangesDownloaded);

                 ///此时客户端的数据已经同步
                customersTableAdapter.Fill(northwindDataSet.Customers);
                new NorthwindDataSetTableAdapters.customersTableAdapter().Fill(northwindDataSet.Customers);
  new NorthwindDataSetTableAdapters.ProductsTableAdapter().Fill(northwindDataSet.Products);
             }

总结

以上可见,通过IDE可以快速的建立一个常用的Sync程序,这个默认的程序只能下载,如果需要双向的同步可参考SDK:

Walkthrough: Extending the Local Database Cache to Support Bidirectional Synchronization

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值