Microsoft sync Frameworks, MSF, is a comprehensive synchronization platform that supports applications, services, devices online, and offline synchronization. MSF consists of the following components: * Sync Services for ADO: Sync data from ADO * Sync Services for FeedSync: Sync RSS and ATOM seed * Syn C Services for file Systems: Synchronizing files and folders * Sync Framework Core Runtime: A customized synchronization scheme that supports native code.
This article focuses on how to quickly establish a sync Services for ADO. NET applications for data synchronization. Since the WinForm project on the PC can quickly establish a synchronization program using the wizard, this article first describes the sync Services for ADO on the PC's WebForm program. NET development, will be introduced on mobile device development, this is my most concerned about the part.
Download installation
Please download Microsoft synchronization Services for ADO at the following link. Microsoft synchronization Services for ADO. NET locally cached data needs to use SQL CE 3.5 and cannot support SQL CE 3.0. Differences in SQL CE 3.5 and 3.0 can refer to the. NET campact framework for SQL CE compatibility issues
Set up local Database Cache
First set up a PC's WinForm program. Right-click the project new "new Item", after the installation of Microsoft Synchronization Services for ADO, the new wizard will appear "Local Database Cache", select the type.
Figure 1
This file can establish a synchronization relationship between the server and the local database, the server database can be any database that supports ADO, I use the free SQL Server Express, the local database is SQL CE 3.5, can not be built manually, The wizard automatically generates the local database based on the server data. SQL Server Express's download installation can refer to SQL Server Express and the SQL Server Compact application to build a library on SQL Server Express called TestDB, build two tables, The table structure is as follows:
CREATE TABLE [dbo]. [T1] ([F1] [Int] Not NULL, [F2] [nvarchar] (50) COLLATE sql_latin1_general_cp1_ci_as not NULL, CONSTRAINT [pk_t1] PRIMARY KEY CLUSTERED ([F1] ASC) with (pad_index= off, ignore_dup_key = off) on [PRIMARY]) on [PRIMARY]
CREATE TABLE [dbo]. [T2] ([F1] [int] NOT NULL, [F2] [datetime] NOT NULL, CONSTRAINT [PK_T2] PRIMARY KEY CLUSTERED ([F1] ASC) with (P Ad_index = off, ignore_dup_key = off) on [PRIMARY]) on [PRIMARY]
Select Database
Figure 2
After configuring the database server, you can select Add to increase the tables that need to be synchronized, and select T1 and T2 in the example.
Figure 3
If the datasheet is in the following case, then it cannot be displayed for selection: 1. The user's default schema is not the original data. 2. No primary key is defined. 3. A name longer than 118 characters. 4. There are data types that are not supported by SQL CE 3.5 in the field.
The wizard automatically adds lasteditdate and CreationDate automatically to the original table under the default selection. Also add data tables (the original table name _tombstone).
Add a data source (dataset)
After adding the Synchronized data table, the wizard automatically pops up the wizard that adds the data source (dataset), fills in the dataset name, and selects the table and fields.
Figure 4
The system automatically generates processing classes and data classes, such as
Figure 5
Code writing
Add the Load event handler to the program and increment a button as well as the handler function, adding the following code:
PublicPartialClassForm1:form {Testdbdataset Testdbdataset=NewTestdbdataset (); Testdbdatasettableadapters.t1tableadapter T1tableadapter=NewTestdbdatasettableadapters.t1tableadapter (); Testdbdatasettableadapters.tableadaptermanager TableAdapterManager=NewTestdbdatasettableadapters.tableadaptermanager ();PublicForm1 () {InitializeComponent (); }
PrivatevoidButton1_Click (Objectsender, EventArgs e) {tableadaptermanager.updateall (testdbdataset);
//Call Syncagent.synchronize () to initiate the synchronization process.//Synchronization only updates the local database and not your project ' s data source. Localdatacache1syncagent syncagent=NewLocaldatacache1syncagent (); Microsoft.Synchronization.Data.SyncStatistics Syncstats= syncagent.synchronize (); TestDbDataSet.t1.Merge (T1tableadapter.getdata ()); }
private void Form1_Load (object sender, EventArgs e) {T1tableadapter.fill (test DBDATASET.T1); } }
A program that synchronizes from server to local SQL CE is completed.
Testdbdataset: Represents a strongly typed memory data cache. T1tableadapter represents the connection and commands used to fetch the database and save the data. TableAdapterManager is used to assist in updating the association relationship between TableAdapters (hierarchical update).
Test
Add some data to the T1 table in SQL Server Express, as follows
Figure 6
Run the program and click the Sync button to sync.
When you open the SQL CE database in VS2008, you will find the SQL CE Library data identical to the T1 table in SQL Server Express.
Modify, remove data from the T1 table in SQL Server Express, and re-test, you will also find the SQL CE Library synchronizing data on the server.
Rapid development Synchronizer under the Microsoft Sync Framework