SqlCeReplication.AddSubscription Method

Creates a new anonymous subscription to an existing SQL Server publication. After calling the AddSubscription method, the application must call the Synchronize method to synchronize the new subscription to the publication based on the latest snapshot.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

Syntax

'Declaration
Public Sub AddSubscription ( _
    addOption As AddOption _
)
public void AddSubscription (
    AddOption addOption
)
public:
void AddSubscription (
    AddOption addOption
)
public void AddSubscription (
    AddOption addOption
)
public function AddSubscription (
    addOption : AddOption
)

Parameters

  • addOption

Remarks

SQL Server Compact 3.5 SP1 replication only supports anonymous subscriptions.

The database administrator must configure SQL Server to support replication, create the SQL Server publication, and enable the publication for anonymous subscriptions before any SQL Server Compact 3.5 SP1 application can subscribe to the publication. The administrator does this on the SQL Server system using either the administrative or programmatic interfaces of SQL Server replication.

The AddOption value specifies the source of the newly created SQL Server Compact 3.5 SP1 subscription database. The value can be one of the following constants:

Value

Description

CreateDatabase

Specifies that the SQL Server Compact 3.5 SP1 database must first be created, and that the subscription contents are then obtained from the publisher over the network. In this case, the AddSubscription and Synchronize method calls create the SQL Server Compact 3.5 SP1 database and download the database contents from the SQL Server Publisher.

ExistingDatabase

Specifies that the database already exists, but contents are obtained from the publisher over the network. In this case, the AddSubscription and Synchronize method calls create the SQL Server Compact 3.5 SP1 subscription and download the database contents from the SQL Server Publisher.

AddOption only affects how the SQL Server Compact 3.5 SP1 database is initially created and treated by the SQL Server Compact 3.5 SP1 Client Agent; therefore, determining what data is downloaded to the Windows Mobile device from the server.

Example

This example creates a new subscription database by passing the CreateDatabase value of AddOption when calling the AddSubscription method.

Dim repl As SqlCeReplication = Nothing

Try
    ' Instantiate and configure SqlCeReplication object
    '
    'NOTE: when possible, prompt users to enter security 
    'credentials at runtime. If you store credentials in a file, 
    'you must secure the file to prevent unauthorized access.
    '
    repl = New SqlCeReplication()
    repl.InternetUrl = "http://www.adventure-works.com/sqlce/sqlcesa35.dll"
    repl.InternetLogin = "MyInternetLogin"
    repl.InternetPassword = "<enterStrongPassword>"
    repl.Publisher = "MyPublisher"
    repl.PublisherDatabase = "MyPublisherDatabase"
    repl.PublisherLogin = "MyPublisherLogin"
    repl.PublisherPassword = "<enterStrongPassword>"
    repl.Publication = "MyPublication"
    repl.Subscriber = "MySubscriber"
    repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf"

    ' Create the local SQL Mobile 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
SqlCeReplication repl = null;

try
{
    // Instantiate and configure SqlCeReplication object
    //
    //NOTE: when possible, prompt users to enter security 
    //credentials at runtime. If you store credentials in a file, 
    //you must secure the file to prevent unauthorized access.
    //
    repl = new SqlCeReplication();
    repl.InternetUrl = "http://www.adventure-works.com/sqlce/sqlcesa35.dll";
    repl.InternetLogin = "MyInternetLogin";
    repl.InternetPassword = "<enterStrongPassword>";
    repl.Publisher = "MyPublisher";
    repl.PublisherDatabase = "MyPublisherDatabase";
    repl.PublisherLogin = "MyPublisherLogin";
    repl.PublisherPassword = "<enterStrongPassword>";
    repl.Publication = "MyPublication";
    repl.Subscriber = "MySubscriber";
    repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";

    // Create a local SQL Mobile 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();
}

Thread Safety

Any public static (Shared in Microsoft Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Version Information
.NET Framework and NET Compact Framework
Supported in 3.5
.NET Framework
Supported in 3.0
.NET Compact Framework and .Net Framework
Supported in 2.0

See Also

Reference

SqlCeReplication Class
SqlCeReplication Members
System.Data.SqlServerCe Namespace