Вы находитесь на странице: 1из 7

5/5/2015

Tutorial: Synchronizing SQL Server and SQL Server Compact [Sync Framework] - TechNet Articles - United States (English) - TechNet Wiki

Tutorial: Synchronizing SQL Server and SQL Server


Compact [Sync Framework]
This tutorial provides step-by-step instructions for implementing data synchronization between a SQL Server database and a SQL Server Compact database using Sync Framework 2.1. It
contains following walkthroughs, which provide detailed steps to perform a specific task within the tutorial.

This topic is a how to.


Please keep it as clear and simple as possible. Avoid speculative discussions as well as a deep dive into underlying
mechanisms or related technologies.

Basic Walkthroughs
Walkthrough

Description

Walkthrough: Creating a simple server


database
Walkthrough: Defining a scope and provisioning
the server databasewith sync artifacts
Walkthrough: Provisioning a SQL Compact
database
Walkthrough: Executing synchronization

In this walkthrough you will create a sample SQL Server database that you will use later in a synchronization scenario with a SQL
Server Compact database.
In this walkthrough you will create a console application that defines a sync scope and provision the SQL Server database that you
created in previous walkthrough with scope related artifacts.
In this walkthrough you will create a SQL Server compact database, which acts as a client for the SQL Server database you created
earlier in this tutorial, and create a console application that provisions the compact database with sync scope related artifacts.
In this walkthrough, you will create a console application that kicks off synchronization process between the SQL Server and SQL
Server compact databases you created in previous walkthroughs.

Walkthrough: Creating a sample server database


In this walkthrough, you will create a sample SQL Server database that you will use later to synchronize with a SQL Server compact database. The following list contains the detailed steps
to create the sample database.
1. Launch SQL Server Management Studio: Click Start, point to Programs, point to Microsoft SQL Server 2008, and then click SQL Server Management Studio. Use similar
steps if you are using SQL Server 2005 or SQL Server 2008 R2 to launch SQL Server Management Studio for that specific version.
2. Connect to SQL Server by using appropriate credentials: On the Connect to Server dialog box, select Database engine for Server type, select a SQL Server instance for Server
name, and use appropriate authentication settings to connect to the server.
3. Click New Query on the toolbar or press Ctrl+N to launch a query window.
4. Copy the following SQL code to the query editor.
USE [master]
GO

IF EXISTS(SELECTnameFROMsys.databases WHEREname= 'SyncDB')


DROPDATABASESyncDB

CREATEDATABASE[SyncDB]
GO

USE [SyncDB]
GO

CREATETABLE[dbo].[Products](
[ID] [int] NOTNULL,
[Name] [nvarchar](50) NOTNULL,
[ListPrice] [money] NOTNULL

CONSTRAINT[PK_Products] PRIMARYKEYCLUSTERED ([ID] ASC)


)
GO

CREATETABLE[dbo].[Orders](
[OrderID] [int] NOTNULL,
[ProductID] [int] NOTNULL,
[Quantity] [int] NOTNULL,
[OriginState] [nvarchar](2) NOTNULL,
CONSTRAINT[PK_Orders] PRIMARYKEYCLUSTERED ([OrderID] ASC,[ProductID] ASC)
)
GO

ALTERTABLE[dbo].[Orders] WITHCHECKADD CONSTRAINT[FK_Orders_Products] FOREIGNKEY([ProductID])


REFERENCES[dbo].[Products] ([ID])
GO

ALTERTABLE[dbo].[Orders] CHECKCONSTRAINT[FK_Orders_Products]
GO

INSERTINTOProducts VALUES(1, 'PC', 400)


INSERTINTOProducts VALUES(2, 'Laptop', 600)
INSERTINTOProducts VALUES(3, 'NetBook', 300)

INSERTINTOOrders VALUES(1, 1, 2, 'NC')


INSERTINTOOrders VALUES(2, 2, 1, 'NC')

http://social.technet.microsoft.com/wiki/contents/articles/2190.tutorial-synchronizing-sql-server-and-sql-server-compact-sync-framework.aspx

1/7

5/5/2015

Tutorial: Synchronizing SQL Server and SQL Server Compact [Sync Framework] - TechNet Articles - United States (English) - TechNet Wiki

INSERTINTOOrders VALUES(3, 1, 5, 'WA')


INSERTINTOOrders VALUES(3, 3, 10, 'WA')
INSERTINTOOrders VALUES(4, 2, 4, 'WA')
5. Press F5 to execute the query.
6. In the Object Explorer pane, right-click <database name> and click Refresh. Expand Databases, and confirm that SyncDB database is created with two tables: dbo.Products and
dbo.Orders.
7. Keep SQL Server Management Studio open to perform next walkthrough.

Walkthrough: Defining a scope and provisioning the server database with sync related artifacts
To prepare the server database for synchronization, you will need to describe a sync scope and provision the server database with scope related artifacts.
By describing a sync scope, you define what you want to synchronize. A sync scope is a set of tables that must be synchronized as a single unit. The tables can already exist in the
database or they can be described by using the Sync Framework object model and then be generated at runtime when the underlying store is provisioned. In this walkthrough, you will
use the Products table that already exists in the server database.
The provisioning of a database involves adding sync scope related artifacts such as tracking tables, triggers, and stored procedures to the database. These artifacts are used by the
synchronization process at runtime. Optionally, the base table is also added to the database as specified in the previous paragraph. For in-depth technical details about provisioning,
seeHow To: Execute Database Synchronization (SQL Server) and Provisioning for Synchronization (SQL Server) .
In this walkthrough you will create a console application that defines a sync scope named ProductsScope, which includes the Products table, and provisions the SQL Server database that
you created in previous walkthrough with sync scope related artifacts.
1. Launch Visual Studio 2008 or Visual Studio 2010: Click Start, point to Programs, point to Microsoft Visual Studio 2008 or Microsoft Visual Studio 2010, and then click
Microsoft Visual Studio 2008 or Microsoft Visual Studio 2010.
2. Click File on menu bar, point to New, and click Project.
3. Select Visual C# from Project Types, and select Console Application from Templates.
4. Type ProvisionServer for the project name, C:\ for Location, and SyncSQLServerAndSQLCompact for Solution Name.
5. Click OK to close the New Project dialog box.
6. In the Solution Explorer window, right-click ProvisionServer, and click Add Reference.
7. Select Microsoft.Synchronization.Data and Microsoft.Synchronization.Data.SqlServer on the .NET tab. These are the assemblies shipped with Microsoft Sync Framework.
8. Click OK to close the Add Reference dialog box.
9. Add the following using statements to the beginning of the Program.cs file after the existing using statements. These namespaces contains the classes that you will be using in
the code for this console application.
usingSystem.Data.SqlClient;
usingMicrosoft.Synchronization.Data;
usingMicrosoft.Synchronization.Data.SqlServer;
10. Add the following statement to the Main method to create a connection to the SyncDB server database. replace the server name with your servers instance name, if you are not
using the default instance. For example: if your SQL Server instance is called MYSQLINSTANCE, replace (local) with .\MYSQLINSTANCE.
SqlConnection serverConn = newSqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated Security=True");
11. Add the following code to the Main method to define a sync scope. This code creates the ProductsScope sync scope, gets the description of Products table in the SyncDB
database, and adds the description to the ProductsScope. The high level steps for defining a sync scope are:
a. Create an instance of the Microsoft.Synchronization.Data.DbSyncScopeDescriptionclass. The Microsoft.Synchronization.Data.DbSyncScopeDescriptionclass is used
to specify the name of the sync scope and the list of tables to be synchronized. The tables are specified using the
Microsoft.Synchronization.Data.DbSyncTableDescription class.
b. Create an instance of the DbSyncTableDescription class based on the schema of Products table retrieved from the SyncDB server database. The
DbSyncTableDescription class is used to specify the name of the table, columns of the table to be synchronized, data types of the table, and other information that is
required for the sync. This information can be specified explicitly or it can be obtained by querying the database using the GetDescriptionForTable(String,
SqlConnection) method. In this walkthrough, you will use the GetDescriptionForTable(String, SqlConnection) method of the SqlSyncDescriptionBuilder class to retrieve the
description of the table
c. Add the DbSyncTableDescription object to Tables collection of the DbSyncScopeDescriptionobject using the Add method.
// define a new scope named ProductsScope
DbSyncScopeDescription scopeDesc = newDbSyncScopeDescription("ProductsScope");

// get the description of the Products table from SyncDB dtabase


DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Products", serverConn);

// add the table description to the sync scope definition


scopeDesc.Tables.Add(tableDesc);
12. Add the following code to the Main method to provision the SyncDB server database with sync related artifacts. This code creates a
Microsoft.Synchronization.Data.SqlServer.SqlSyncScopeProvisioning object, skips the creation of Products table on the server since the table already exists, and provisions the
SyncDB database with scope related artifacts. The high level steps for provisioning the server database with sync scope related artifacts are:
a. Create an instance of the Microsoft.Synchronization.Data.DbSyncScopeDescription class. The Microsoft.Synchronization.Data.DbSyncScopeDescription class is used
to specify the name of the sync scope and the list of tables to be synchronized. The tables are specified using the
Microsoft.Synchronization.Data.DbSyncTableDescriptionclass.
b. Invoke the SetCreateTableDefault(DbSyncCreationOption)method by specifying the DbSyncCreationOption value as Skip because the Products table already exists in
the server database. The SetCreateTableDefault(DbSyncCreationOption) method is used to specify whether to create base tables when a scope is configured.
c. Invoke the Apply() method on SqlSyncScopeProvisioning object to start the provisioning process, which creates the change-tracking infrastructure in the server database.
// create a server scope provisioning object based on the ProductScope
SqlSyncScopeProvisioning serverProvision = newSqlSyncScopeProvisioning(serverConn, scopeDesc);

// skipping the creation of table since table already exists on server


serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);

// start the provisioning process


serverProvision.Apply();
13. In Solution Explorer, right-click ProvisionServer, and click Build.
14. Press Ctrl+F5 to execute the program.
15. Press ENTER to close the command prompt window.
16. In SQL Server Management Studio, expand SyncDB node, expand Tables, and you should see the following additional tables created by the provisioning process:
Products_Tracking, schema_info, scope_config, and scope_info. There are also other database objects such as triggers and stored procedures created by the provisioning
process.
17. Keep Visual Studio open and SQL Server Management open.
18. Complete Code Example:
usingSystem.Data.SqlClient;
usingMicrosoft.Synchronization.Data;
usingMicrosoft.Synchronization.Data.SqlServer;

http://social.technet.microsoft.com/wiki/contents/articles/2190.tutorial-synchronizing-sql-server-and-sql-server-compact-sync-framework.aspx

2/7

5/5/2015

Tutorial: Synchronizing SQL Server and SQL Server Compact [Sync Framework] - TechNet Articles - United States (English) - TechNet Wiki

namespaceProvisionServer
{
classProgram
{
staticvoidMain(string[] args)
{
SqlConnection serverConn = newSqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated Security=True");

// define a new scope named ProductsScope


DbSyncScopeDescription scopeDesc = newDbSyncScopeDescription("ProductsScope");

// get the description of the Products table from SyncDB dtabase


DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Products", serverConn);

// add the table description to the sync scope definition


scopeDesc.Tables.Add(tableDesc);

// create a server scope provisioning object based on the ProductScope


SqlSyncScopeProvisioning serverProvision = newSqlSyncScopeProvisioning(serverConn, scopeDesc);

// skipping the creation of table since table already exists on server


serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);

// start the provisioning process


serverProvision.Apply();
}
}
}

Walkthrough: Provisioning a SQL Compact client database

In this walkthrough you will create a SQL Server Compact database named SyncCompactDB, and create a console application that provisions the compact database with ProductsScope
related artifacts. The provision process prepares the client database for synchronization with the server. In the previous walkthrough, you provisioned the server database with sync related
artifacts already.

To create a SQL Server Compact database


The following list contains steps to create the SyncCompactDB compact database, which will be used as a client for the SyncDB server database.

1.
2.
3.
4.
5.
6.

In SQL Server Management Studio, click File menu, and click Connect Object Explorer.
In the Connect to Server dialog box, select SQL Server Compact Edition for Server type.
Click down-arrow next to Database file, and click New Database.
In the Create New SQL Server Compact Database dialog box, type C:\SyncSQLServerAndSQLCompact\SyncCompactDB.sdf for Database file name, and click OK.
Click Yes on the message box you see about the blank password
Click Connect on the Connect to Server dialog box.

To provision SQL Server Compact database


The following list contains steps to create a console application using Visual Studio to provision the SQL Server Compact database SyncCompactDB with artifacts related to sync scope
ProductsScope.

1.
2.
3.
4.
5.
6.
7.
8.

9.

10.

11.

In Visual Studio, In Solution Explorer, right-click Solution SyncSQLServerAndSQLCompact, point to Add, and click New Project.
Select Visual C# from Project Types, and select Console Application from Templates.
Type ProvisionClient for project name.
Click OK to close the New Project dialog box.
In Solution Explorer window, right-click ProvisionClient, and click Add Reference.
Select Microsoft.Synchronization.Data, Microsoft.Synchronization.Data.SqlServer, Microsoft.Synchronization.Data.SqlServerCe and click OK to close the
Add Reference dialog box.
Repeat previous two steps to add a reference to System.Data.SqlServerCe assembly.
Add the following using statements to the beginning of the Program.cs file after the existing using statements.
usingSystem.Data.SqlClient;
usingSystem.Data.SqlServerCe;

usingMicrosoft.Synchronization.Data;
usingMicrosoft.Synchronization.Data.SqlServer;
usingMicrosoft.Synchronization.Data.SqlServerCe;
Add the following statement to the Main method to create a SQL connection to the compact database.
// create a connection to the SyncCompactDB database
SqlCeConnection clientConn = newSqlCeConnection(@"Data Source='C:\SyncSQLServerAndSQLCompact\SyncCompactDB.sdf'");
Add the following statement to the Main method to create a SQL connection to the server database. Replace the server name with your servers instance name,
if you are not using the default instance. For example: if your SQL Server instance is called MYSQLINSTANCE, replace (local) with .\MYSQLINSTANCE.
// create a connection to the SyncDB server database
SqlConnection serverConn = newSqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated
Security=True");
Add the following statement to the Main method to get the description of the ProductsScope scope from the SQL Server. This statement invokes the
SqlCeSyncDescriptionBuilder.GetDescriptionForScope method on the Microsoft.Synchronization.Data.SqlServer.SqlSyncDescriptionBuilder class to
retrieve description of the ProductsScope from server.
TheMicrosoft.Synchronization.Data.DbSyncScopeDescription class is used to specify the name of the sync scope and the list of tables to be synchronized.

http://social.technet.microsoft.com/wiki/contents/articles/2190.tutorial-synchronizing-sql-server-and-sql-server-compact-sync-framework.aspx

3/7

5/5/2015

12.

13.
14.
15.
16.
17.

Tutorial: Synchronizing SQL Server and SQL Server Compact [Sync Framework] - TechNet Articles - United States (English) - TechNet Wiki

This information can be specified explicitly or it can be obtained by querying the database using the SqlSyncDescriptionBuilder.GetDescriptionForScope
method. In this walkthrough, you will use the SqlSyncDescriptionBuilder.GetDescriptionForScope method of the
Microsoft.Synchronization.Data.SqlServer.SqlSyncDescriptionBuilder class to retrieve the description of the scope from the server.
// get the description of ProductsScope from the SyncDB server database
DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope("ProductsScope", serverConn);
Add the following statements to provision the SQL Server compact database with the ProductsScope related artifacts. The high level steps for provisioning a
SQL Server Compact database with sync scope related artifacts are:
a. Create an instance of the Microsoft.Synchronization.Data.SqlServerCe.SqlCeSyncScopeProvisioning class based on the
Microsoft.Synchronization.Data.DbSyncScopeDescription obtained in the previous step and a connection to the compact database. The
Microsoft.Synchronization.Data.SqlServerCe.SqlCeSyncScopeProvisioning class represents the provisioning of a SQL Server Compact database for a
particular scope that is represented by a Microsoft.Synchronization.Data.DbSyncScopeDescription object.
b. Invoke the Apply method on SqlCeSyncScopeProvisioningobject to start the provisioning process, which creates the change-tracking infrastructure in
the compact database.
// create CE provisioning object based on the ProductsScope
SqlCeSyncScopeProvisioning clientProvision = newSqlCeSyncScopeProvisioning(clientConn, scopeDesc);

// starts the provisioning process


clientProvision.Apply();
In Solution Explorer, right-click ProvisionClient, and click Build.
In Solution Explorer, right-click ProvisionClient again, and click Set as Startup Project. If you do not set the current project as a startup project and press
Ctrl+F5 again, the ProvisionServer application is executed again, and you get an error message similar to the following: scope that already exists in the server
database.
Press Ctrl+F5 to execute the program.
Press ENTER to close the command prompt window.
In SQL Server Management Studio, expand SQL Server Compact [My Computer\...\SyncCompactDB] node, expand Tables, and you should see the following
additional tables created by the provisioning process: Products_Tracking, schema_info, scope_config, and scope_info. There are other objects such as triggers
and stored procedures created by the provisioning process
Keep Visual Studio and SQL Server Management Studio open.

18.
19. Complete Code Example:

usingSystem.Data.SqlClient;
usingSystem.Data.SqlServerCe;

usingMicrosoft.Synchronization.Data;
usingMicrosoft.Synchronization.Data.SqlServer;
usingMicrosoft.Synchronization.Data.SqlServerCe;

namespaceProvisionClient
{
classProgram
{
staticvoidMain(string[] args)
{
// create a connection to the SyncCompactDB database
SqlCeConnection clientConn = newSqlCeConnection(@"Data Source='C:\SyncSQLServerAndSQLCompact\SyncCompactDB.sdf'");

// create a connection to the SyncDB server database


SqlConnection serverConn = newSqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated Security=True");

// get the description of ProductsScope from the SyncDB server database


DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope("ProductsScope", serverConn);

// create CE provisioning object based on the ProductsScope


SqlCeSyncScopeProvisioning clientProvision = newSqlCeSyncScopeProvisioning(clientConn, scopeDesc);

// starts the provisioning process


clientProvision.Apply();

}
}
}

Executing Synchronization
In previous two walkthroughs you prepared the server database SyncDB and the compact database SyncCompactDB for synchronization. In this walkthrough, you will
create a console application that kicks off synchronization process between these two databases. For in-depth technical details about provisioning servers/clients and
executing the synchronization process, see How To: Execute Database Synchronization (SQL Server) .
1.
2.
3.
4.
5.
6.

In Visual Studio, In Solution Explorer, right-click Solution SyncSQLServerAndSQLCompact, point to Add, and click New Project.
Select Visual C# from Project Types, and select Console Application from Templates.
Type ExecuteCompactSync for project name.
Click OK to close the New Project dialog box.
In Solution Explorer window, right-click ExecuteCompactSync, and click Add Reference.
Select Microsoft.Synchronization, Microsoft.Synchronization.Data, Microsoft.Synchronization.Data.SqlServer,
Microsoft.Synchronization.Data.SqlServerCe and click OK to close the Add Reference dialog box.
7. Repeat previous two steps to add a reference to System.Data.SqlServerCe assembly.
8. Add the following using statements to the beginning of the Program.cs file after the existing using statements.
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Data.SqlServerCe;

http://social.technet.microsoft.com/wiki/contents/articles/2190.tutorial-synchronizing-sql-server-and-sql-server-compact-sync-framework.aspx

4/7

5/5/2015

9.

10.

11.

12.

13.

14.

15.

16.
17.
18.

Tutorial: Synchronizing SQL Server and SQL Server Compact [Sync Framework] - TechNet Articles - United States (English) - TechNet Wiki

usingMicrosoft.Synchronization;
usingMicrosoft.Synchronization.Data;
usingMicrosoft.Synchronization.Data.SqlServer;
usingMicrosoft.Synchronization.Data.SqlServerCe;
Add the following statement to the Main method to create a SQL connection to the compact database.
// create a connection to the SyncCompactDB database
SqlCeConnection clientConn = newSqlCeConnection(@"Data Source='C:\SyncSQLServerAndSQLCompact\SyncCompactDB.sdf'");
Add the following statement to the Main method to create a SQL connection to the server database. Replace the server name with your servers instance name,
if you are not using the default instance. For example: if your SQL Server instance is called MYSQLINSTANCE, replace (local) with .\MYSQLINSTANCE.
// create a connection to the SyncDB server database
SqlConnection serverConn = newSqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated
Security=True");
Add the following code to the Main method to create a sync orchestrator, which initiates and controls synchronization sessions. The sync orchestrator contains
two sync providers that will participate in a synchronization session. In our scenario, you will need to use a provider object for the server database and a
provider object for the compact client database. The high level steps of creating an orchestrator for this scenario are:
a. Create an instance of the Microsoft.Synchronization.SyncOrchestrator class. The SyncOrchestrator class initiates and controls synchronization sessions.
b. Set the local provider of the sync orchestrator object to a Microsfot.Synchronization.Data.SqlServerCe.SqlCeSyncProvider object associated with the
SyncCompactDB client database. The SqlCeSyncProvider class encapsulates a synchronization provider for SQL Server Compact that communicates with
the client and shields the synchronization orchestrator from the specific implementation of the client database.
c. Set the remote provider of the sync orchestrator to a Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider object associated with the SyncDB
server database. The SqlSyncProvider class represents a synchronization provider that communicates with a SQL Server database and shields other Sync
Framework components from the specific implementation of the database.
d. Set the sync direction of sync orchestrator object to SyncDirectionOrder.UploadAndDownload so that the client can download/upload changes from/to
the server.
// create the sync orhcestrator
SyncOrchestrator syncOrchestrator = newSyncOrchestrator();

// set local provider of orchestrator to a CE sync provider associated with the


// ProductsScope in the SyncCompactDB compact client database
syncOrchestrator.LocalProvider = newSqlCeSyncProvider("ProductsScope", clientConn);

// set the remote provider of orchestrator to a server sync provider associated with
// the ProductsScope in the SyncDB server database
syncOrchestrator.RemoteProvider = newSqlSyncProvider("ProductsScope", serverConn);

// set the direction of sync session to Upload and Download


syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;
Add the following code to subscribe for any errors that occur when applying changes to the client. The SqlCeClientSyncProvider.ApplyChangeFailed event is
raised when a row could not be applied at a client. You will be defining the handler for the error event later in this walkthrough.
// subscribe for errors that occur when applying changes to the client
((SqlCeSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new
EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
Add the following code to the Main method. This code invokes the Synchronize method on theSyncOrchestrator object to start the synchronization process
between the SyncDB server database and SyncCompactDB compact database.
// execute the synchronization process
SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();
Add the following statements to the Main method to display statistics returned by theSyncOrchestrator.Synchronize method. The
Microsoft.Synchronization.SyncOperationStatistics object returned by this method contains statistics about the synchronization session that was executed.
// print statistics
Console.WriteLine("Start Time: "+ syncStats.SyncStartTime);
Console.WriteLine("Total Changes Uploaded: "+ syncStats.UploadChangesTotal);
Console.WriteLine("Total Changes Downloaded: "+ syncStats.DownloadChangesTotal);
Console.WriteLine("Complete Time: "+ syncStats.SyncEndTime);
Console.WriteLine(String.Empty);
Add the following event handler method to the Program class after the Main method to handle the ApplyChangeFailed event. The
DbApplyChangeFailedEventArgs parameter provides information about the error or conflict that caused the failure. In a handler for the event, you can respond
to the event in several ways, including specifying whether the synchronization provider should try to apply the row again. The Error property of the object
contains metadata about any exceptions that occurred during synchronization. The following sample code displays this error and the type of conflict (of
typeDbConflictType) occurred during synchronization.
staticvoidProgram_ApplyChangeFailed(objectsender, DbApplyChangeFailedEventArgs e)
{
// display conflict type
Console.WriteLine(e.Conflict.Type);

// display error message


Console.WriteLine(e.Error);
}
In Solution Explorer, right-click ExecuteCompactSync, and click Build.
In Solution Explorer, right-click ExecuteCompactSync again, and click Set as Startup Project. If you do not perform this step and you press Ctrl+F5 again, the
ProvisionClient application is executed again, and you get an error message about the scope that already exists in the client database.
Press Ctrl+F5 to execute the program. You should see the following output. Note that 3 records were downloaded and added to the client database.
Start Time: 6/14/2010 6:24:19 PM
Total Changes Uploaded: 0
Total Changes Downloaded: 3
Complete Time: 6/14/2010 6:24:22 PM

http://social.technet.microsoft.com/wiki/contents/articles/2190.tutorial-synchronizing-sql-server-and-sql-server-compact-sync-framework.aspx

5/7

5/5/2015

Tutorial: Synchronizing SQL Server and SQL Server Compact [Sync Framework] - TechNet Articles - United States (English) - TechNet Wiki

Press any key to continue. . .


19. Press ENTER to close the command prompt window.
20. In SQL Server Management Studio, right-click SQL Server Compact [My Computer\...\SyncCompactDB] node, and click New Query to open a query window
21. Type and execute (by pressing F5) the following SQL command to confirm that records are indeed downloaded to the compact client.
select* fromproducts
22. In the SQL Server Management Studio, expand (local) SQL Server, expand Databases, expand SyncDB, and expand Tables.
23. Right-click dbo.Products, and click Edit Top 200 Rows.
24. Add a record at the end with 4 as ID, Wireless Mouse for Name, and 45 for ListPrice. Make sure the record is saved by pressing TAB after entering the last
value.
25. Now, in Visual Studio, press Ctrl+F5 to execute the client program again. You should see output similar to the following output. The one record you added to
the server database should be downloaded to the client.
Start Time: 6/14/2010 6:32:44 PM
Total Changes Uploaded: 0
Total Changes Downloaded: 1
Complete Time: 6/14/2010 6:32:47 PM

Press any key to continue. . .


26. In SQL Server Management Studio, select SQL Server Compact [My Computer\...\SyncCompactDB] node.
27. Click New Query from the toolbar.
28. Type and execute (by pressing F5) the following SQL command to confirm that records are indeed downloaded to the compact client.
select* fromproducts
29. You can play around to become familiar with the Sync Framework technology by adding/updating/deleting records from the server/client. For example, if you
delete a record from the server, the corresponding record in the client database should be deleted next time you sync client with the server.
30. Keep Visual Studio and SQL Server Management Studio open if you want to perform walkthroughs that follow this walkthrough in the tutorial.
31. Complete Code Example:
usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Data.SqlServerCe;

usingMicrosoft.Synchronization;
usingMicrosoft.Synchronization.Data;
usingMicrosoft.Synchronization.Data.SqlServer;
usingMicrosoft.Synchronization.Data.SqlServerCe;

namespaceExecuteCompactSync
{
classProgram
{
staticvoidMain(string[] args)
{
// create a connection to the SyncCompactDB database
SqlCeConnection clientConn = newSqlCeConnection(@"Data
Source='C:\SyncSQLServerAndSQLCompact\SyncCompactDB.sdf'");

// create a connection to the SyncDB server database


SqlConnection serverConn = newSqlConnection("Data Source=localhost; Initial Catalog=SyncDB; Integrated
Security=True");

// create the sync orhcestrator


SyncOrchestrator syncOrchestrator = newSyncOrchestrator();

// set local provider of orchestrator to a CE sync provider associated with the


// ProductsScope in the SyncCompactDB compact client database
syncOrchestrator.LocalProvider = newSqlCeSyncProvider("ProductsScope", clientConn);

// set the remote provider of orchestrator to a server sync provider associated with
// the ProductsScope in the SyncDB server database
syncOrchestrator.RemoteProvider = newSqlSyncProvider("ProductsScope", serverConn);

// set the direction of sync session to Upload and Download


syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

// subscribe for errors that occur when applying changes to the client
((SqlCeSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new
EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);

// execute the synchronization process


SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();

// print statistics
Console.WriteLine("Start Time: "+ syncStats.SyncStartTime);
Console.WriteLine("Total Changes Uploaded: "+ syncStats.UploadChangesTotal);
Console.WriteLine("Total Changes Downloaded: "+ syncStats.DownloadChangesTotal);
Console.WriteLine("Complete Time: "+ syncStats.SyncEndTime);

http://social.technet.microsoft.com/wiki/contents/articles/2190.tutorial-synchronizing-sql-server-and-sql-server-compact-sync-framework.aspx

6/7

5/5/2015

Tutorial: Synchronizing SQL Server and SQL Server Compact [Sync Framework] - TechNet Articles - United States (English) - TechNet Wiki
Console.WriteLine(String.Empty);
}

staticvoidProgram_ApplyChangeFailed(objectsender, DbApplyChangeFailedEventArgs e)
{
// display conflict type
Console.WriteLine(e.Conflict.Type);

// display error message


Console.WriteLine(e.Error);
}
}
}

http://social.technet.microsoft.com/wiki/contents/articles/2190.tutorial-synchronizing-sql-server-and-sql-server-compact-sync-framework.aspx

7/7

Вам также может понравиться