Jaa


How to: Create a SQL Server Compact Edition Database by Using the Replication Object (Programmatically)

In this topic, you will learn how to programmatically create a Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) database by calling the AddSubscription method of the SqlServerCe.Replication object. For more information about using the SqlServerCe namespace, see the SqlServerCe namespace reference documentation.

To create a SQL Server Compact Edition database by using the Replication object

  1. Initialize a new Replication object.

    SqlCeReplication repl = new SqlCeReplication();
    
  2. Set the Replication object properties. These properties can include the information needed to connect to the SQL Server Publisher. The SubscriberConnectionString property specifies the file name and location for the database that will be created.

    repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";
    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";
    
  3. Call the AddSubscription method, passing in the AddOption.CreateDatabase parameter.

    repl.AddSubscription(AddOption.CreateDatabase);
    

Example

This example shows how to create a new database by creating a Replication object, setting the properties for the database and subscription, and then calling the AddSubscription method.

SqlCeReplication repl = null;
        try
        {
            // Instantiate and configure SqlCeReplication object
            //
            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";

            // Create a local SQL Server Compact Edition 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();
        }
Dim repl As SqlCeReplication = Nothing
        Try
            ' Instantiate and configure SqlCeReplication object
            '
            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"

            ' Create the local SQL Server Compact Edition Database subscription
            '
            repl.AddSubscription(AddOption.CreateDatabase)

            ' Synchronize to the 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

Help and Information

Getting SQL Server Compact Edition Assistance