建立發行集

適用於:SQL ServerAzure SQL 受控執行個體

本文說明如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (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 驗證,您也必須為 @publisher_security_mode 指定值 0,並為 @publisher_login@publisher_password 指定 SQL Server 登入資訊。 請繼續進行步驟 3。

  3. 在發行者端,執行 sp_addpublication (Transact-SQL)。 針對 @publication指定發行集名稱,然後針對 @repl_freq 參數,指定快照式發行集的 snapshot 值或交易式發行集的 continuous 值。 指定任何其他發行集選項。 這樣會定義此發行集。

    注意

    發行集名稱不能包含下列字元:

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

  4. 在發行者端,執行 sp_addpublication_snapshot (Transact-SQL)。 為 @publication 指定步驟 2 中所使用的發行集名稱,以及針對 @snapshot_job_name @password @password中啟用交易式發行集的訂閱更新。 如果代理程式會在連接到發行者時使用 SQL Server 驗證,您也必須為 @publisher_security_mode 指定值 0,並為 @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 @password中啟用交易式發行集的訂閱更新。 如果代理程式會在連接到發行者時使用 SQL Server 驗證,您也必須為 @publisher_security_mode 指定值 0,並為 @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

使用 Replication Management Objects (RMO)

您可以使用 Replication Management Objects (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 類別的執行個體,並為此物件設定下列屬性:

  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

另請參閱