Quick Development and synchronization programs under Microsoft sync framework

Source: Internet
Author: User
Tags sql server express

Microsoft sync frameworks (MSF) is a comprehensive synchronization platform that supports applications.Program, Services, online and offline synchronization of devices. MSF consists of the following components:
* Sync services for ADO. Net: Synchronize ADO. NET data.
* Sync services for feedsync: Synchronize RSS and atom Seeds
* Sync services for file systems: Synchronize files and folders
* Sync framework core runtime: a custom synchronization solution that supports native code.

This article describes how to quickly create a sync services for ADO. NET application for data synchronization. Since you can quickly create a synchronization program in the PC winform project using the wizard, This article first introduces the sync services for ado on the PC webform program.. NET development will introduce the development on mobile devices one after another, which is also my biggest concern.

Download and install

Go to the following link to download microsoft synchronization services for ADO. net. SQL ce 3.5 is required for Microsoft synchronization services for ADO. Net local cache data. SQL CE 3.0 is not supported. For the differences between SQL ce 3.5 and 3.0, see SQL ce compatibility issues under. Net campact framework.

Create a local database cache

First, create a winform program for the PC. Right-click the project to create a new item. After installing Microsoft synchronization services for ADO. net, the "local database cache" appears in the new Wizard. Select this type.


Figure 1

This file establishes the synchronization relationship between the server and the local database. The server database supports ADO. net any database, I use free SQL Server Express, the local database is SQL ce 3.5, you do not need to manually create, the Wizard program automatically generate a local database based on server data. For how to download and install SQL Server Express, referSQL Server express and SQL Server compact applications
Create a database named testdb on SQL Server express and create two tables with the following table structure:

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 (pad_index=Off, ignore_dup_key=Off) on [primary]
) On [primary]

 

Select Database

 

Figure 2

After configuring the database server, you can select "add" to add tables to be synchronized. In this example, select T1 and T2.

Figure 3

If the data table belongs to the following conditions, it cannot be displayed for selection:
1. It is not the user's default schema ).
2. No primary key is defined.
3. The name must be longer than 118 characters.
4. The fields contain data types not supported by SQL ce 3.5.

By default, the wizard automatically adds lasteditdate and creationdate to the original table. Add a data table (former table name_tombstone ).

Add a data source (Dataset)

After adding a Data Synchronization table, the wizard automatically displays the add data source (Dataset) wizard, fill in the dataset name, select the table and field.


Figure 4

The system automatically generates processing and data classes, as shown in figure

Figure 5

Code Write

 

Add the load event handler and a button and handler in the program. Add the following code:

 

Public   Partial   Class Form1: Form
{
Testdbdataset =   New Testdbdataset ();
Testdbdatasettableadapters. t1tableadapter t1tableadapter =   New Testdbdatasettableadapters. t1tableadapter ();
Testdbdatasettableadapters. tableadaptermanager =   New Testdbdatasettableadapters. tableadaptermanager ();

Public Form1 ()
{
Initializecomponent ();
}

Private VoidButton#click (ObjectSender, eventargs E)
{
Tableadaptermanager. updateall (testdbdataset );

// Call syncagent. Synchronize () to initiate the synchronization process.
// Synchronization only updates the local database, not your project's data source.
Localdatache1syncagent syncagent =   New Localdatache1syncagent ();
Microsoft. synchronization. Data. syncstatistics syncstats = Syncagent. Synchronize ();
Testdbdataset. t1.merge (t1tableadapter. getdata ());
}

Private VoidForm1_load (ObjectSender, eventargs E)
{
T1tableadapter. Fill (testdbdataset. T1 );
}
}

A program synchronized from the server to the local SQL CE is completed.

Testdbdataset: indicates a strong type of memory data cache.
T1tableadapter indicates the connection and commands used to fetch and store data.
Tableadaptermanager is used to update the association between tableadapters (hierarchical update ).

Test

Add some data to table T1 in SQL Server express as follows:

Figure 6

Run the program and click synchronize.

Open the SQL ce database in vs2008 and you will find that the data in the SQL ce database is exactly the same as that in Table T1 in SQL Server Express.

Modify and delete the data in Table T1 in SQL Server express for further testing. You will also find that the SQL ce database synchronizes data on the server.

 

References
Overview and scenarios (sync services)

Sync framework Overview

How to choose a Data Synchronization Technology-Offline & collaboration
Sync 101-getting started with sync framework v2

Sync101-(Windows Mobile) device Synchronization

Introduction to ADO. Net sync services

Jake's blog in blog Park -- simplified development of wireless living

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.