Share via


How to: Create a Database by Using the Replication Object (Programmatically)

In this topic, you will learn how to programmatically create a Microsoft SQL Server Compact 3.5 (SQL Server Compact 3.5) 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.

Procedures for SQL Server Compact 3.5

To create a 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/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";
    
  3. Call the AddSubscription method, passing in the AddOption.CreateDatabase parameter.

    repl.AddSubscription(AddOption.CreateDatabase);
    

To create a case-sensitive 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. In addition, the SubscriberConnectionString property supports a new "Case sensitive" property. This property is used to create a case-sensitive subscription database. For more information about case-sensitive databases, see Working with Collations (SQL Server Compact).

    repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf; LCID=1033; Case Sensitive=true";
    repl.InternetUrl = "https://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";
    
  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/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();
        }
Dim repl As SqlCeReplication = Nothing
        Try
            ' Instantiate and configure SqlCeReplication object
            '
            repl = New SqlCeReplication()
            repl.InternetUrl = "https://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 the local 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 Assistance (SQL Server Compact 3.5 Service Pack 1)