Tutorial - Synchronizing SQL Server and SQL Server Compact (Sync Framework) PDF
Tutorial - Synchronizing SQL Server and SQL Server Compact (Sync Framework) PDF
Tutorial: Synchronizing SQL Server and SQL Server Compact [Sync Framework] - TechNet Articles - United States (English) - TechNet Wiki
Basic Walkthroughs
Walkthrough
Description
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.
CREATEDATABASE[SyncDB]
GO
USE [SyncDB]
GO
CREATETABLE[dbo].[Products](
[ID] [int] NOTNULL,
[Name] [nvarchar](50) NOTNULL,
[ListPrice] [money] NOTNULL
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] CHECKCONSTRAINT[FK_Orders_Products]
GO
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
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");
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");
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.
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.
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);
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'");
}
}
}
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 the remote provider of orchestrator to a server sync provider associated with
// the ProductsScope in the SyncDB server database
syncOrchestrator.RemoteProvider = newSqlSyncProvider("ProductsScope", serverConn);
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
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'");
// 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);
// subscribe for errors that occur when applying changes to the client
((SqlCeSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new
EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
// 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);
http://social.technet.microsoft.com/wiki/contents/articles/2190.tutorial-synchronizing-sql-server-and-sql-server-compact-sync-framework.aspx
7/7