Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Programming Merge Replication with the Microsoft .NET Compact Framework

.NET Compact Framework 1.0

Content Master Ltd

April 2003

Applies to:
    Microsoft® .NET Compact Framework 1.0
    Microsoft Visual Studio® .NET 2003

Summary: Learn how to programmatically invoke merge replication in your .NET Compact Framework-based applications. (4 printed pages)


The SqlCeReplication Class
Subscribing to Published Data
Synchronizing with the Publisher
More Information


Merge replication allows you to synchronize data stored in SQL Server CE subscription database with its publisher. Invoking merge replication programmatically is useful in scenarios where ad-hoc network connections are used for smart devices. Such devices may not be available to the network all the time, and so must invoke merge replication when it is convenient.

The .NET Compact Framework’s Data Provider for SQL Server CE contains the SqlCeReplication class. This class can be used to programmatically invoke merge replication and manage subscriptions to published databases.

This article assumes you have already created a snapshot directory and publication in SQL Server 2000, and have used the Configure Connectivity Support in IIS option on the SQL Server CE 2.0 programs menu option on your server to configure a virtual directory in your web server. Also, familiarity with developing Smart Device applications in Visual Studio .NET 2003 is assumed.

The SqlCeReplication Class

Merge replication allows each subscription database to be updated autonomously. Each subscription database uses change tracking to keep track of INSERT, UPDATE, and DELETE statements executed on the Windows CE device. These changes are then replicated up to the database publisher.

In SQL Server CE, merge replication uses the SqlCeReplication class, which is part of the .NET Compact Framework Data Provider for SQL Server CE. This class enables you to manage subscription and snapshot synchronization with SQL Server 2000 publications.

The SqlCeReplication class contains the following methods:

  • AddSubscription: This is used to create a new subscription to a published database and, optionally, to create the local SQL Server CE database.
  • DropSubscription: This is used to remove an existing subscription to a published database. Also, this method can remove the local SQL Server CE subscription database.
  • ReinitializeSubscription: This is used to mark a subscription for re-initialization. Re-initialization re-creates the snapshot of the published database on the publishing server.
  • Synchronize: This is used to invoke the merge replication between the subscriber and publisher.

Subscribing to Published Data

The first stage of merge replication is to create a subscription database on the smart device and create a subscription to a published database on the server. This is achieved by creating a SqlCeReplication object, passing some initial property values to its constructor and then calling the AddSubscription method. The properties you need to set are listed in the table below:

Property Description
InternetURL The URL to sscesa20.dll, which is in the virtual directory you set up in the Configure Connectivity Support in IIS wizard
InternetLogin Required only if Anonymous authentication is not used
InternetPassword Required only if Anonymous authentication is not used
Publisher Name of the publishing server. If this is an instance of SQL Server 2000, it will be in the format ServerName/InstanceName
PublisherDatabase Name of the published database
Publication Name of the publication on the published database
Subscriber Name of your subscription
SubscriberConnectonString Local connection string for your SQL Server CE database file (*sdf)

After calling AddSubscription, your local SQL Server CE database will have been created, but the tables and their initial snapshot of the data will not be present. You must call the Synchronize method to transfer the initial snapshot of data to the new SQL Server CE database.

Synchronizing with the Publisher

The Synchronize method of the SqlCeReplication class will retrieve the initial snapshot of data for your local SQL Server CE database if it is the first time you call it, or if it is the first time after the publication has been marked for re-initialization. Once the initialization of data has taken place, subsequent calls will invoke merge replication, only sending and receiving the changes that have been made to the data.

The following sample code shows a subscription being set up only if the local database does not exist. It is worth noting that the code disposes of the SqlCeReplication object at the end, but does not drop the subscription. The next time the code is executed, there is no data initialization.

// Create a new instance of the SqlCeReplication class
SqlCeReplication rep = new SqlCeReplication(
    @"http://myserver/pubsmr/sscesa20.dll", // InternetURL
    "CETest","cetest", // InternetLogin and InternetPassword
    "MYSERVER", // Publisher
    "pubs", // PublisherDatabase
    "pubs", // Publication
    "Testing", // Subscriber
    @" Data Source=\My Documents\PubsMR.sdf"); // ConnectionString
    // Check if the database file already exists
    if(!System.IO.File.Exists(@"\My Documents\PubsMR.sdf"))
        // Add a new subscripton and create the local database file
    // Transfer the initial snapshot of data if this is the first
    // time is this called. Subsequent calls will transfer only the
    // changes to the data
catch(SqlCeException ex)
    // Display any errors in a message box
    // Dispose of the SqlCeReplication object, but don’t drop the
    // subscription
    // Notify the user that merge replication is complete
    MessageBox.Show("Replication complete");

Once you have created a subscription and the initial transfer of snapshot data has been used to create the local database, you can use the classes contained within the System.Data.SqlServerCe namespace to update, insert, and delete data. Any changes you make will be merged into the published database the next time you call the Synchronize method.

More Information

For further information, please see the resources below:

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.