创建发布

适用于:SQL ServerAzure SQL 托管实例

本项目介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象 (RMO) 在 SQL Server 中创建发布。

限制和局限

  • 发布和项目名称不能包含下列任何字符:%*[]|:"?'\/<>。 如果数据库中的对象包括任意上述字符,且希望将其复制,那么必须在“项目属性 - <项目>”对话框(可从向导中的“项目”页获取)中指定与对象名称不同的项目名称。

安全性

如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Microsoft Windows .NET Framework 提供的加密服务

使用 SQL Server Management Studio

可以使用新建发布向导创建发布和定义项目。 创建发布之后,可在“发布属性 - <发布>”对话框中查看和修改发布属性。 有关从 Oracle 数据库创建发布的信息,请参阅从 Oracle 数据库创建发布

创建发布并定义项目

  1. 在 Microsoft SQL Server Management Studio 中连接到发布服务器,然后展开服务器节点。

  2. 展开 “复制” 文件夹,再右键单击 “本地发布” 文件夹。

  3. 选择“新建发布”

  4. 按照新建发布向导中的页完成以下任务:

    • 如果尚未在服务器上配置分发,请指定分发服务器。 有关如何配置分发的详细信息,请参阅配置发布和分发

      如果在“分发服务器”页上指定将发布服务器用作其自己的分发服务器(本地分发服务器),而未将服务器配置为分发服务器,则新建发布向导将配置该服务器。 在 “快照文件夹” 页中指定分发服务器的快照文件夹。 快照文件夹只是指定共享的目录。向此文件夹中执行读写操作的代理必须对其具有足够的访问权限。 有关正确保护文件夹的详细信息,请参阅保护快照文件夹

      如果指定另一台服务器作为分发服务器,则必须在 “管理密码” 页上输入密码来连接发布服务器和分发服务器。 此密码必须与在远程分发服务器上启用发布服务器时所指定的密码相匹配。

      有关详细信息,请参阅 Configure Distribution

    • 选择发布数据库。

    • 选择发布类型。 有关详细信息,请参阅复制类型

    • 指定要发布的数据和数据库对象;(可选)筛选来自表项目的列,并设置项目属性。

    • 可选择筛选来自表项目的行。 有关详细信息,请参阅筛选已发布数据

    • 设置快照代理调度。

    • 指定运行下列复制代理和进行连接的凭证:

    • (可选)编写发布脚本。 有关详细信息,请参阅 Scripting Replication

    • 指定发布的名称。

使用 Transact-SQL

可以使用复制存储过程以编程方式创建发布。 使用的存储过程将取决于要创建的发布的类型。

创建快照或事务发布

  1. 在发布数据库的发布服务器上,执行 sp_replicationdboption (Transact-SQL) 以允许使用快照或事务复制发布当前数据库。

  2. 对于事务发布,确定发布数据库中是否存在日志读取器代理作业。 (快照发布不需要此步骤)。

    • 如果发布数据库中存在日志读取器代理作业,则继续执行步骤 3。

    • 如果无法确定已发布的数据库是否存在日志读取器代理作业,请在发布数据库中的发布服务器上执行 sp_helplogreader_agent (Transact-SQL)

    • 如果结果集为空,则创建日志读取器代理作业。 在发布服务器上,执行 sp_addlogreader_agent (Transact-SQL)。 指定为 @job_name@password 运行代理时的 Microsoft Windows 凭据。 如果代理在连接到发布服务器时使用 SQL Server 身份验证,则还必须将 0 的值指定为 @publisher_security_mode,为 @publisher_login@publisher_password 指定 SQL Server 登录信息。 继续执行步骤 3。

  3. 在发布服务器上,执行 sp_addpublication (Transact-SQL)。 为 @publication@repl_freq 参数指定发布名称,将快照发布的值指定为 snapshot 或将事务发布的值指定为 continuous。 指定任何其他发布选项。 这便定义了发布。

    注意

    发布名称不能包括下列字符:

    %*[]|:"?\/<>

  4. 在发布服务器上,执行sp_addpublication_snapshot (Transact-SQL)。 将步骤 3 中使用的发布名称指定给 @publication 指定在步骤 2 中使用的发布名称,并为 @snapshot_job_name@password中对事务发布启用更新订阅。 如果代理在连接到发布服务器时使用 SQL Server 身份验证,则还必须将 0 的值指定为 @publisher_security_mode,为 @publisher_loginpublisher_password 指定 SQL Server 登录信息。 此操作将为发布创建一个快照代理作业。

    重要

    使用远程分发服务器配置发布服务器时,为所有参数提供的值(包括 job_loginjob_password)都会以纯文本方式发送到该分发服务器。 在执行此存储过程之前,应该对发布服务器及其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)

  5. 向发布添加项目。 有关详细信息,请参阅 定义项目

  6. 启动快照代理作业以为此发布生成初始快照。 有关详细信息,请参阅 创建并应用初始快照

创建合并发布

  1. 在发布服务器上,执行 sp_replicationdboption (Transact-SQL),以允许使用合并复制发布当前数据库。

  2. 在发布数据库的发布服务器上,执行 sp_addmergepublication (Transact-SQL)。 为 @publication 指定发布的名称,并指定任何其他发布选项。 这便定义了发布。

    注意

    发布名称不能包括下列字符:

    %*[]|:"?\/<>

  3. 在发布服务器上,执行sp_addpublication_snapshot (Transact-SQL)。 为 @publication 指定在步骤 2 中使用的发布名称,并为 @snapshot_job_name@password中对事务发布启用更新订阅。 如果代理在连接到发布服务器时使用 SQL Server 身份验证,则还必须将 0 的值指定为 @publisher_security_mode,为 @publisher_login@publisher_password 指定 SQL Server 登录信息。 此操作将为发布创建一个快照代理作业。

    重要

    使用远程分发服务器配置发布服务器时,为所有参数提供的值(包括 job_loginjob_password)都会以纯文本方式发送到该分发服务器。 在执行此存储过程之前,应该对发布服务器及其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)

  4. 向发布添加项目。 有关详细信息,请参阅 定义项目

  5. 启动快照代理作业以为此发布生成初始快照。 有关详细信息,请参阅 创建并应用初始快照

示例 (Transact-SQL)

此示例创建事务发布。 脚本变量用于传递创建快照代理和日志读取器代理作业时所需的 Windows 凭据。

-- To avoid storing the login and password in the script file, the values 
-- are passed into SQLCMD as scripting variables. For information about 
-- how to use scripting variables on the command line and in SQL Server
-- Management Studio, see the "Executing Replication Scripts" section in
-- the topic "Programming Replication Using System Stored Procedures".

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks'; 
SET @publication = N'AdvWorksProductTran'; 
-- Windows account used to run the Log Reader and Snapshot Agents.
SET @login = $(Login); 
-- This should be passed at runtime.
SET @password = $(Password); 

-- Enable transactional or snapshot replication on the publication database.
EXEC sp_replicationdboption 
    @dbname=@publicationDB, 
    @optname=N'publish',
    @value = N'true';

-- Execute sp_addlogreader_agent to create the agent job. 
EXEC sp_addlogreader_agent 
    @job_login = @login, 
    @job_password = @password,
    -- Explicitly specify the use of Windows Integrated Authentication (default) 
    -- when connecting to the Publisher.
    @publisher_security_mode = 1;

-- Create a new transactional publication with the required properties. 
EXEC sp_addpublication 
    @publication = @publication, 
    @status = N'active',
    @allow_push = N'true',
    @allow_pull = N'true',
    @independent_agent = N'true';

-- Create a new snapshot job for the publication, using a default schedule.
EXEC sp_addpublication_snapshot 
    @publication = @publication, 
    @job_login = @login, 
    @job_password = @password,
    -- Explicitly specify the use of Windows Integrated Authentication (default) 
    -- when connecting to the Publisher.
    @publisher_security_mode = 1;
GO

此示例创建合并发布。 脚本变量用于传递创建快照代理作业时所需的 Windows 凭据。

-- To avoid storing the login and password in the script file, the value 
-- is passed into SQLCMD as a scripting variable. For information about 
-- how to use scripting variables on the command line and in SQL Server
-- Management Studio, see the "Executing Replication Scripts" section in
-- the topic "Programming Replication Using System Stored Procedures".

--Declarations for adding a merge publication
DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks2022'; 
SET @publication = N'AdvWorksSalesOrdersMerge'; 
SET @login = $(Login);
SET @password = $(Password);

-- Enable merge replication on the publication database, using defaults.
USE master
EXEC sp_replicationdboption 
  @dbname=@publicationDB, 
  @optname=N'merge publish',
  @value = N'true' 

-- Create a new merge publication, explicitly setting the defaults. 
USE [AdventureWorks2022]
EXEC sp_addmergepublication 
-- These parameters are optional.
  @publication = @publication,
  -- optional parameters 
  @description = N'Merge publication of AdventureWorks2022.',
  @publication_compatibility_level  = N'120RTM';

-- Create a new snapshot job for the publication.
EXEC sp_addpublication_snapshot 
  @publication = @publication, 
  @job_login = @login, 
  @job_password = @password;
GO

使用复制管理对象 (RMO)

可以使用复制管理对象 (RMO) 以编程方式创建发布。 用于创建发布的 RMO 类取决于所创建发布的类型。

创建快照或事务发布

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 为发布数据库创建 ReplicationDatabase 类的实例,将 ConnectionContext 属性设置为步骤 1 中的 ServerConnection 的实例,然后调用 LoadProperties 方法。 如果 LoadProperties 返回 false,请验证该数据库是否存在。

  3. 如果 EnabledTransPublishing 属性为 false,请将其设置为 true

  4. 对于事务发布,请检查 LogReaderAgentExists 属性的值。 如果该属性为 true,说明已经存在一个针对此数据库的日志读取器代理作业。 如果该属性为 false,请执行如下操作:

  5. 创建 TransPublication 类的实例,并设置此对象的以下属性:

  6. 调用 Create 方法来创建发布。

    重要

    在使用远程分发服务器配置发布服务器时,为所有属性提供的值(包括 SnapshotGenerationAgentProcessSecurity)都会以纯文本形式发送到该分发服务器。 调用 Create 方法之前,应先对发布服务器与其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)

  7. 调用 CreateSnapshotAgent 方法,为发布创建快照代理作业。

创建合并发布

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 为发布数据库创建 ReplicationDatabase 类的实例,将 ConnectionContext 属性设置为步骤 1 中的 ServerConnection 的实例,然后调用 LoadProperties 方法。 如果 LoadProperties 返回 false,请验证该数据库是否存在。

  3. 如果 EnabledMergePublishing 属性为 false,请将其设置为 true,然后调用 CommitPropertyChanges

  4. 创建 MergePublication 类的实例,并设置此对象的以下属性:

    • ServerConnection 设置为步骤 1 中的 ConnectionContext

    • DatabaseName设置为已发布的数据库的名称。

    • Name属性设置为发布的名称。

    • 设置 LoginPasswordSnapshotGenerationAgentProcessSecurity 字段,为运行快照代理所用的 Windows 帐户提供凭据。 如果使用 Windows 身份验证,在快照代理连接到本地分发服务器或建立远程连接时,也会使用此帐户。

      注意

      如果发布是由 SnapshotGenerationAgentProcessSecurity 固定服务器角色的成员创建的,则不需要设置 P:Microsoft.SqlServer.Replication.ReplicationDatabase.LogReaderAgentProcessSecurity 。 有关详细信息,请参阅 复制代理安全模式

    • (可选)使用“或”逻辑 OR 运算符(在 Visual C# 中为 |,在 Visual Basic 中为 Or)和“异或”逻辑 OR 运算符(在 Visual C# 中为 ^,在 Visual Basic 中为 Xor),将 PublicationAttributes 属性的值设置为 Attributes

  5. 调用 Create 方法来创建发布。

    重要

    在使用远程分发服务器配置发布服务器时,为所有属性提供的值(包括 SnapshotGenerationAgentProcessSecurity)都会以纯文本形式发送到该分发服务器。 调用 Create 方法之前,应先对发布服务器与其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)

  6. 调用 CreateSnapshotAgent 方法,为发布创建快照代理作业。

示例 (RMO)

本示例启用 AdventureWorks 数据库进行事务发布,定义日志读取器代理作业并创建 AdvWorksProductTran 发布。 必须为此发布定义一个项目。 创建日志读取器代理作业和快照代理作业所需的 Windows 帐户凭据在运行时进行传递。 若要了解如何使用 RMO 定义快照项目和事务项目,请参阅 Define an Article

// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2022";
string publisherName = publisherInstance;

ReplicationDatabase publicationDb;
TransPublication publication;

// Create a connection to the Publisher using Windows Authentication.
ServerConnection conn;
conn = new ServerConnection(publisherName);


try
{
    // Connect to the Publisher.
    conn.Connect();

    // Enable the AdventureWorks2022 database for transactional publishing.
    publicationDb = new ReplicationDatabase(publicationDbName, conn);

    // If the database exists and is not already enabled, 
    // enable it for transactional publishing.
    if (publicationDb.LoadProperties())
    {
        if (!publicationDb.EnabledTransPublishing)
        {
            publicationDb.EnabledTransPublishing = true;
        }

        // If the Log Reader Agent does not exist, create it.
        if (!publicationDb.LogReaderAgentExists)
        {
            // Specify the Windows account under which the agent job runs.
            // This account will be used for the local connection to the 
            // Distributor and all agent connections that use Windows Authentication.
            publicationDb.LogReaderAgentProcessSecurity.Login = winLogin;
            publicationDb.LogReaderAgentProcessSecurity.Password = winPassword;

            // Explicitly set authentication mode for the Publisher connection
            // to the default value of Windows Authentication.
            publicationDb.LogReaderAgentPublisherSecurity.WindowsAuthentication = true;

            // Create the Log Reader Agent job.
            publicationDb.CreateLogReaderAgent();
        }
    }
    else
    {
        throw new ApplicationException(String.Format(
            "The {0} database does not exist at {1}.",
            publicationDb, publisherName));
    }

    // Set the required properties for the transactional publication.
    publication = new TransPublication();
    publication.ConnectionContext = conn;
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;

    // Specify a transactional publication (the default).
    publication.Type = PublicationType.Transactional;

    // Activate the publication so that we can add subscriptions.
    publication.Status = State.Active;

    // Enable push and pull subscriptions and independent Distribition Agents.
    publication.Attributes |= PublicationAttributes.AllowPull;
    publication.Attributes |= PublicationAttributes.AllowPush;
    publication.Attributes |= PublicationAttributes.IndependentAgent;

    // Specify the Windows account under which the Snapshot Agent job runs.
    // This account will be used for the local connection to the 
    // Distributor and all agent connections that use Windows Authentication.
    publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin;
    publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword;

    // Explicitly set the security mode for the Publisher connection
    // Windows Authentication (the default).
    publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true;

    if (!publication.IsExistingObject)
    {
        // Create the transactional publication.
        publication.Create();

        // Create a Snapshot Agent job for the publication.
        publication.CreateSnapshotAgent();
    }
    else
    {
        throw new ApplicationException(String.Format(
            "The {0} publication already exists.", publicationName));
    }
}

catch (Exception ex)
{
    // Implement custom application error handling here.
    throw new ApplicationException(String.Format(
        "The publication {0} could not be created.", publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' Set the Publisher, publication database, and publication names.
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2022"
Dim publisherName As String = publisherInstance

Dim publicationDb As ReplicationDatabase
Dim publication As TransPublication

' Create a connection to the Publisher using Windows Authentication.
Dim conn As ServerConnection
conn = New ServerConnection(publisherName)

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Enable the AdventureWorks2022 database for transactional publishing.
    publicationDb = New ReplicationDatabase(publicationDbName, conn)

    ' If the database exists and is not already enabled, 
    ' enable it for transactional publishing.
    If publicationDb.LoadProperties() Then
        If Not publicationDb.EnabledTransPublishing Then
            publicationDb.EnabledTransPublishing = True
        End If

        ' If the Log Reader Agent does not exist, create it.
        If Not publicationDb.LogReaderAgentExists Then
            ' Specify the Windows account under which the agent job runs.
            ' This account will be used for the local connection to the 
            ' Distributor and all agent connections that use Windows Authentication.
            publicationDb.LogReaderAgentProcessSecurity.Login = winLogin
            publicationDb.LogReaderAgentProcessSecurity.Password = winPassword

            ' Explicitly set authentication mode for the Publisher connection
            ' to the default value of Windows Authentication.
            publicationDb.LogReaderAgentPublisherSecurity.WindowsAuthentication = True

            ' Create the Log Reader Agent job.
            publicationDb.CreateLogReaderAgent()
        End If
    Else
        Throw New ApplicationException(String.Format( _
         "The {0} database does not exist at {1}.", _
         publicationDb, publisherName))
    End If

    ' Set the required properties for the transactional publication.
    publication = New TransPublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' Specify a transactional publication (the default).
    publication.Type = PublicationType.Transactional

    'Enable push and pull subscriptions and independent Distribition Agents.
    publication.Attributes = _
    publication.Attributes Or PublicationAttributes.AllowPull
    publication.Attributes = _
    publication.Attributes Or PublicationAttributes.AllowPush
    publication.Attributes = _
    publication.Attributes Or PublicationAttributes.IndependentAgent

    ' Activate the publication so that we can add subscriptions.
    publication.Status = State.Active

    ' Specify the Windows account under which the Snapshot Agent job runs.
    ' This account will be used for the local connection to the 
    ' Distributor and all agent connections that use Windows Authentication.
    publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin
    publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword

    ' Explicitly set the security mode for the Publisher connection
    ' Windows Authentication (the default).
    publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = True

    If Not publication.IsExistingObject Then
        ' Create the transactional publication.
        publication.Create()

        ' Create a Snapshot Agent job for the publication.
        publication.CreateSnapshotAgent()
    Else
        Throw New ApplicationException(String.Format( _
            "The {0} publication already exists.", publicationName))
    End If
Catch ex As Exception
    ' Implement custom application error handling here.
    Throw New ApplicationException(String.Format( _
        "The publication {0} could not be created.", publicationName), ex)
Finally
    conn.Disconnect()
End Try

本示例启用 AdventureWorks 数据库进行合并发布,并创建 AdvWorksSalesOrdersMerge 发布。 仍然必须为此发布定义项目。 创建快照代理作业所需的 Windows 帐户凭据在运行时进行传递。 若要了解如何使用 RMO 定义合并项目,请参阅 Define an Article

// Set the Publisher, publication database, and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks2022";

ReplicationDatabase publicationDb;
MergePublication publication;

// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Enable the database for merge publication.				
    publicationDb = new ReplicationDatabase(publicationDbName, conn);
    if (publicationDb.LoadProperties())
    {
        if (!publicationDb.EnabledMergePublishing)
        {
            publicationDb.EnabledMergePublishing = true;
        }
    }
    else
    {
        // Do something here if the database does not exist. 
        throw new ApplicationException(String.Format(
            "The {0} database does not exist on {1}.",
            publicationDb, publisherName));
    }

    // Set the required properties for the merge publication.
    publication = new MergePublication();
    publication.ConnectionContext = conn;
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;

    // Enable precomputed partitions.
    publication.PartitionGroupsOption = PartitionGroupsOption.True;

    // Specify the Windows account under which the Snapshot Agent job runs.
    // This account will be used for the local connection to the 
    // Distributor and all agent connections that use Windows Authentication.
    publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin;
    publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword;

    // Explicitly set the security mode for the Publisher connection
    // Windows Authentication (the default).
    publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true;

    // Enable Subscribers to request snapshot generation and filtering.
    publication.Attributes |= PublicationAttributes.AllowSubscriberInitiatedSnapshot;
    publication.Attributes |= PublicationAttributes.DynamicFilters;

    // Enable pull and push subscriptions.
    publication.Attributes |= PublicationAttributes.AllowPull;
    publication.Attributes |= PublicationAttributes.AllowPush;

    if (!publication.IsExistingObject)
    {
        // Create the merge publication.
        publication.Create();
        
        // Create a Snapshot Agent job for the publication.
        publication.CreateSnapshotAgent();
    }
    else
    {
        throw new ApplicationException(String.Format(
            "The {0} publication already exists.", publicationName));
    }
}

catch (Exception ex)
{
    // Implement custom application error handling here.
    throw new ApplicationException(String.Format(
        "The publication {0} could not be created.", publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' Set the Publisher, publication database, and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2022"

Dim publicationDb As ReplicationDatabase
Dim publication As MergePublication

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Enable the database for merge publication.				
    publicationDb = New ReplicationDatabase(publicationDbName, conn)
    If publicationDb.LoadProperties() Then
        If Not publicationDb.EnabledMergePublishing Then
            publicationDb.EnabledMergePublishing = True
        End If
    Else
        ' Do something here if the database does not exist. 
        Throw New ApplicationException(String.Format( _
         "The {0} database does not exist on {1}.", _
         publicationDb, publisherName))
    End If

    ' Set the required properties for the merge publication.
    publication = New MergePublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' Enable precomputed partitions.
    publication.PartitionGroupsOption = PartitionGroupsOption.True

    ' Specify the Windows account under which the Snapshot Agent job runs.
    ' This account will be used for the local connection to the 
    ' Distributor and all agent connections that use Windows Authentication.
    publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin
    publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword

    ' Explicitly set the security mode for the Publisher connection
    ' Windows Authentication (the default).
    publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = True

    ' Enable Subscribers to request snapshot generation and filtering.
    publication.Attributes = publication.Attributes Or _
        PublicationAttributes.AllowSubscriberInitiatedSnapshot
    publication.Attributes = publication.Attributes Or _
        PublicationAttributes.DynamicFilters

    ' Enable pull and push subscriptions
    publication.Attributes = publication.Attributes Or _
        PublicationAttributes.AllowPull
    publication.Attributes = publication.Attributes Or _
        PublicationAttributes.AllowPush

    If Not publication.IsExistingObject Then
        ' Create the merge publication.
        publication.Create()

        ' Create a Snapshot Agent job for the publication.
        publication.CreateSnapshotAgent()
    Else
        Throw New ApplicationException(String.Format( _
            "The {0} publication already exists.", publicationName))
    End If
Catch ex As Exception
    ' Implement custom application error handling here.
    Throw New ApplicationException(String.Format( _
        "The publication {0} could not be created.", publicationName), ex)
Finally
    conn.Disconnect()
End Try

另请参阅