SQL Server 2008 R2

In this topic, you will learn how to create a subscription on Microsoft SQL Server Compact 3.5 by using the SqlCeReplication class, and then perform the initial data synchronization.

To create a subscription and synchronize data

  1. Initialize a SqlCeReplication object.

    SqlCeReplication repl = new SqlCeReplication();
    
  2. Set the connection properties. These properties specify the name and location of the Publication to which you are subscribing, the name and location of the local SQL Server Compact 3.5 database, and the location of the SQL Server Compact 3.5 Server Agent.

    repl.InternetUrl = "http://www.adventure-works.com/sqlmobile/sqlcesa35.dll";
    repl.InternetLogin = "MyInternetLogin";
    repl.InternetPassword = "<password>";
    repl.Publisher = "MyPublisher";
    repl.PublisherDatabase = "MyPublisherDatabase";
    repl.PublisherLogin = "MyPublisherLogin";
    repl.PublisherPassword = "<password>";
    repl.Publication = "MyPublication";
    repl.Subscriber = "MySubscriber";
    repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";
    
  3. Call the AddSubscription method to create the subscription.

    repl.AddSubscription(AddOption.CreateDatabase);
    
  4. Call the Synchronize method to perform data synchronization and populate the local database with the data from the Publication.

    repl.Synchronize();
    

This example shows how to create a subscription and synchronize data with SQL Server. In this example, the local database is named MyDatabase.sdf and the Publication is named MyPublication.

SqlCeReplication repl = null;

try
{
   // Instantiate and configure SqlCeReplication object
   //
   repl = new SqlCeReplication();
   repl.InternetUrl = "http://www.adventure-works.com/sqlmobile/sqlcesa35.dll";
   repl.InternetLogin = "MyInternetLogin";
   repl.InternetPassword = "<password>";
   repl.Publisher = "MyPublisher";
   repl.PublisherDatabase = "MyPublisherDatabase";
   repl.PublisherLogin = "MyPublisherLogin";
   repl.PublisherPassword = "<password>";
   repl.Publication = "MyPublication";
   repl.Subscriber = "MySubscriber";
   repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";

   // Create a local database subscription
   //
   repl.AddSubscription(AddOption.CreateDatabase);

   // Synchronize to the SQL Server database
   //
   repl.Synchronize();
}
catch (SqlCeException)
{
   // Handle errors here
   //
}
finally
{
   // Dispose the repl object
   //
   repl.Dispose();
}

Show: