How to: Reinitialize a SQL Server Compact Edition Subscription (Programmatically)
In this topic, you will learn how to reinitialize a subscription on Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) by using the SqlCeReplication class. For more information about using the SqlServerCe namespace, see the SqlServerCe namespace reference documentation.
To reinitialize a subscription
Initialize a SqlCeReplication object.
SqlCeReplication repl = new SqlCeReplication();
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 Edition database, and the location of the SQL Server Compact Edition Server Agent.
repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.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";
Call the ReinitializeSubscription method to mark the subscription for reinitialization. If you pass true to the ReinitializeSubscription method, changes on the Subscriber are uploaded to the Publisher before reinitialization occurs. If you pass false, any changes on the Subscriber are discarded during reinitialization.
repl.ReinitializeSubscription(true);
To repopulate the subscription, you must call the Synchronize method after calling the ReinitializeSubscription method.
repl.Synchronize();
Example
This example shows how to reinitialize a SQL Server Compact Edition subscription. In this example, any changes on the Subscriber are first uploaded to the Publisher, the subscription is reinitialized, and then the data is synchronized.
SqlCeReplication repl = null;
try
{
// Create SqlCeReplication instance
//
repl = new SqlCeReplication();
repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.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";
// Mark the subscription for reinitialization with Upload first
repl.ReinitializeSubscription(true);
// Synchronize to SQL Server to populate the Subscription
repl.Synchronize();
}
catch (SqlCeException)
{
// Handle errors here
}
finally
{
// Dispose the repl object
repl.Dispose();
}
Dim repl As SqlCeReplication = Nothing
Try
' Create SqlCeReplication instance
repl = New SqlCeReplication()
repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.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"
' Mark the subscription for reinitialization with Upload first
repl.ReinitializeSubscription(True)
' Synchronize to SQL Server to populate the Subscription
repl.Synchronize()
Catch
' Handle errors here
Finally
' Dispose the repl object
repl.Dispose()
End Try
See Also
Other Resources
Using Merge Replication
Subscribing to Publications (SQL Server Compact Edition)
Reinitializing a Subscription (SQL Server Compact Edition)