共用方式為


建立出版物

本主題說明如何在 SQL Server 2014 中使用 SQL Server Management Studio、Transact-SQL 或複寫管理物件 (RMO) 來建立發行項。

本主題內容

開始之前

限制與制約

  • 發行集和發行項名稱不能包含下列任何字元:% 、* 、 [ 、 ] 、 |, : , " , ? 、' 、 \ 、 / 、 < 、 >。 如果資料庫中的物件包含其中任何一個字元,且您想要複製它們,必須在精靈的 [發行項屬性 - <發行項>] 對話框中指定一個和物件名稱不同的發行項名稱,該對話框可以在精靈的 [發行項] 頁中找到。

安全

可能的話,會在執行階段提示使用者輸入安全性認證。 如果您必須儲存認證,請使用 Microsoft Windows .NET Framework 提供的密碼編譯服務

使用 SQL Server Management Studio

使用 [新增發行集精靈] 建立發行集並定義文章。 建立發行集之後,請在 [ 發行集屬性 - <發行集] 對話框中檢視和修改發行集> 屬性。 如需從 Oracle 資料庫建立發行集的詳細資訊,請參閱 從 Oracle 資料庫建立發行集

建立出版物並定義文章

  1. 在 Microsoft SQL Server Management Studio 中連接到發行者,然後展開伺服器節點。

  2. 展開 複寫 資料夾,然後滑鼠右鍵點擊 本機發行集 資料夾。

  3. 點擊新發行

  4. 請按照「新出版物精靈」中的頁面,進行下列操作:

    • 如果未在伺服器上設定散發,請指定散發者。 若要取得設定分發的詳細資訊,請參閱 設定發佈和分發

      如果您在 [散發者] 頁面上指定發行者伺服器將作為自己的散發者(本機散發者),但伺服器尚未設定為「散發者」,[新增發行集精靈] 會為您設定伺服器。 您將在 [快照資料夾] 頁面上指定散發者的預設快照資料夾。 快照集資料夾只是您指定為共享的目錄;讀取和寫入此資料夾的代理程式必須有足夠的許可權才能存取它。 如需適當保護資料夾的詳細資訊,請參閱 保護快照集資料夾

      如果您指定另一部伺服器應該做為「散發者」,您必須在 [ 系統管理密碼 ] 頁面上輸入密碼,以取得從發行者到散發者的連線。 此密碼必須符合在遠端散發者端啟用發行者時所指定的密碼。

      如需詳細資訊,請參閱 配置發佈

    • 選擇出版資料庫。

    • 選取發行集類型。 如需詳細資訊,請參閱 複寫類型

    • 指定要發佈的數據和資料庫物件;選擇性地篩選數據表發行項中的數據行,並設定發行項屬性。

    • 可以選擇篩選資料表中的資料列。 如需詳細資訊,請參閱 篩選已發佈的數據

    • 設定快照代理程式排程。

    • 指定執行下列複寫代理程式並建立連線所需的認證:

      - 所有發行項目的快照代理程式。

      - 用於所有事務性發行集的記錄讀取器代理程式。

      - 用於允許更新訂閱的交易式發行集的佇列讀取器代理程式。

      如需詳細資訊,請參閱 複寫代理程式安全性模型復寫安全性最佳做法

    • 選擇性地編寫發佈腳本。 如需詳細資訊,請參閱 Scripting Replication

    • 指定出版物的名稱。

使用 Transact-SQL

您可以透過程式設計,使用復寫儲存程序來建立出版物。 使用的預存程式將取決於所建立的發行集類型。

建立快照或交易性發行集

  1. 在發行資料庫中的發行者端,執行 sp_replicationdboption (Transact-SQL),以使用快照式或事務性複製來啟用目前資料庫的發行。

  2. 針對交易式發行集,確定發行集資料庫中是否有記錄讀取器代理作業。 (快照式發行集不需要此步驟。

    • 如果發行資料庫存在記錄讀取器代理程式作業,請繼續進行步驟 3。

    • 如果您不確定發行資料庫是否存在日誌讀取器代理程式工作,請在發行資料庫的發行者執行sp_helplogreader_agent(Transact-SQL)

    • 如果結果集合為空,請建立日誌讀取代理作業。 在發行者上,執行 sp_addlogreader_agent(Transact-SQL)。 指定代理程式在Microsoft Windows下執行的認證,包括@job_name和@password。 如果代理程式在連接到發行者時會使用 SQL Server 驗證,您也必須針對 @publisher_security_mode 指定 0 的值,以及@publisher_login@publisher_password的 Microsoft SQL Server 登入資訊。 請繼續進行步驟 3。

  3. 在發行者上,執行 sp_addpublication (Transact-SQL)。 為 @publication指定發行集名稱,針對 @repl_freq 參數指定快照式發行集的值 snapshot 或交易式發行集的值 continuous 。 指定任何其他發行選項。 這定義了出版物。

    備註

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

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

  4. 在發行者上,執行 sp_addpublication_snapshot (Transact-SQL) 。 指定步驟 3 中用於 @publication 的發行集名稱,以及快照集代理程式針對 @snapshot_job_name@password執行的 Windows 認證。 如果代理程式在連接到發行者時會使用 SQL Server 驗證,您也必須針對 @publisher_security_mode 指定 0 值,以及@publisher_login@publisher_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) 。 指定在步驟 2 中用於 @publication 的發行名稱,以及快照代理程式運行的 @snapshot_job_name@password 的 Windows 憑證。 如果代理程式在連接到發行者時會使用 SQL Server 驗證,您也必須針對 @publisher_security_mode 指定 0 值,以及@publisher_login@publisher_password的 SQL Server 登入資訊。 這會為發行建立快照代理程式工作。

    這很重要

    當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_loginjob_password) 都會以純文字的方式傳給散發者。 您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。 如需詳細資訊,請參閱啟用資料庫引擎的加密連線 (SQL Server 組態管理員)

  4. 將文章新增到出版物中。 如需詳細資訊,請參閱 定義發行項

  5. 啟動快照代理作業,以產生此發行的初始快照。 如需詳細資訊,請參閱 建立和套用初始快照集

範例 (Transact-SQL)

此範例用於建立一個交易型的發行項目。 腳本變數用於傳遞建立 Snapshot Agent 和 Log Reader Agent 作業所需的 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'AdventureWorks2012'; 
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 [AdventureWorks2012]
EXEC sp_addmergepublication 
-- These parameters are optional.
  @publication = @publication,
  -- optional parameters 
  @description = N'Merge publication of AdventureWorks2012.',
  @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)

您可以使用複寫管理物件 (RMO) 以程式設計方式建立發行集。 您用來建立發行集的 RMO 類別取決於您建立的發行集類型。

建立快照或交易型發行物

  1. 使用 ServerConnection 類別建立與發行者的連接。

  2. 建立發行集資料庫的 類別實例 ReplicationDatabase 、將 屬性設定 ConnectionContext 為步驟 1 中的 ServerConnection 實例,然後呼叫 LoadProperties 方法。 如果 LoadPropertiesfalse回 ,請確認資料庫存在。

  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 方法。 如果 LoadPropertiesfalse回 ,請確認資料庫存在。

  3. 如果 EnabledMergePublishing Property 為 false,請將它設定為 true,然後呼叫 CommitPropertyChanges

  4. 建立 類別的 MergePublication 實例,併為此物件設定下列屬性:

  5. 呼叫Create方法以建立發行。

    這很重要

    在使用遠程分發者設定發行者時,所有屬性的值,包括 SnapshotGenerationAgentProcessSecurity,都會以純文本形式傳送至分發者。 您應該先加密發行者與其遠端散發者之間的連線,再呼叫 Create 方法。 如需詳細資訊,請參閱啟用資料庫引擎的加密連線 (SQL Server 組態管理員)

  6. 呼叫CreateSnapshotAgent方法以建立發行的快照代理作業。

範例 (RMO)

此範例會啟用 AdventureWorks 資料庫以進行交易式發行、定義記錄讀取器代理程序作業,並建立 AdvWorksProductTran 發行集。 必須為此刊物定義一篇文章。 建立日誌讀取器代理程式作業和快照代理程式作業所需的 Windows 帳戶認證會在執行時傳遞。 若要瞭解如何使用 RMO 來定義快照式和交易式項目,請參閱定義項目

// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2012";
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 AdventureWorks2012 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 = "AdventureWorks2012"
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 AdventureWorks2012 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 來定義合併發行項,請參閱 定義發行項

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

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 = "AdventureWorks2012"

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

另請參閱

搭配腳本變數使用 sqlcmd
發行資料和資料庫物件
複寫管理物件概念
定義文章
檢視和修改發行集屬性
設定散發
保護分銷商
保護發行者