AddSubscription 方法

创建对现有 SQL Server 发布内容的新的匿名订阅。调用 AddSubscription 方法后,应用程序必须调用 Synchronize 方法来根据最新的快照同步对发布内容的新订阅。

命名空间:  System.Data.SqlServerCe
程序集:  System.Data.SqlServerCe(在 System.Data.SqlServerCe.dll 中)

语法

声明
Public Sub AddSubscription ( _
    addOption As AddOption _
)
用法
Dim instance As SqlCeReplication
Dim addOption As AddOption

instance.AddSubscription(addOption)
public void AddSubscription(
    AddOption addOption
)
public:
void AddSubscription(
    AddOption addOption
)
member AddSubscription : 
        addOption:AddOption -> unit 
public function AddSubscription(
    addOption : AddOption
)

参数

注释

SQL Server Compact 3.5 复制仅支持匿名订阅。

数据库管理员必须将 SQL Server 配置为支持复制,创建 SQL Server 发布,并为匿名订阅启用发布,然后 SQL Server Compact 3.5 应用程序才能订阅发布。管理员可在 SQL Server 系统上使用 SQL Server 复制的管理界面或编程界面来完成这些操作。

AddOption 值指定新创建的 SQL Server Compact 3.5 订阅数据库的源。该值可以是下列常量之一:

说明

CreateDatabase

指定必须首先创建 SQL Server Compact 3.5 数据库,然后才能通过网络从发布服务器获取订阅内容。在这种情况下,调用 AddSubscription 和 Synchronize 方法会创建 SQL Server Compact 3.5 数据库并从 SQL Server 发布服务器下载数据库内容。

ExistingDatabase

指定数据库已存在,但其内容需要通过网络从发布服务器获取。在这种情况下,调用 AddSubscription 和 Synchronize 方法会创建 SQL Server Compact 3.5 订阅并从 SQL Server 发布服务器下载数据库内容。

AddOption 只影响 SQL Server Compact 3.5 客户端代理最初创建和处理 SQL Server Compact 3.5 数据库的方式;因此,它也决定了将哪些数据从服务器下载到 Windows Mobile 设备。

示例

此示例通过在调用 AddSubscription 方法时传递 CreateDatabase 作为 AddOption 的值来创建新的订阅数据库。

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 = "https://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 = "https://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();
}

请参阅

参考

SqlCeReplication类

SqlCeReplication 成员

System.Data.SqlServerCe 命名空间