建立發行集

本主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO) 來建立 SQL Server 2012 中的發行集。

本主題內容

  • 開始之前:

    限制事項

    安全性

  • 若要建立發行集並定義發行項,請使用:

    SQL Server Management Studio

    Transact-SQL

    Replication Management Objects (RMO)

開始之前

限制事項

  • 發行集與發行項名稱不能包含下列任何字元:% , * , [ , ] , | , : , " , ? , ' , \ , / , < , >. 如果資料庫中的物件包含這些字元的任何一個且您要複寫它們,則必須指定一個不同於 [發行項屬性 - <Article>] 對話方塊中物件名稱的發行項名稱,此對話方塊可在精靈中的 [發行項] 頁面取得。

安全性

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

搭配回到頁首連結使用的箭頭圖示[Top]

使用 SQL Server Management Studio

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

若要建立發行集並定義發行項

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

  2. 展開 [複寫] 資料夾,然後以滑鼠右鍵按一下 [本機發行集] 資料夾。

  3. 按一下 [新增發行集]

  4. 遵循「新增發行集精靈」中的頁面,執行:

    • 如果尚未在伺服器上設定散發,則請指定「散發者」。 如需設定散發的詳細資訊,請參閱<設定發行和散發>。

      如果在 [散發者] 頁面指定「發行者」伺服器做為自己的「散發者」(本機散發者),而不將伺服器設定為「散發者」,則「新增發行集精靈」會設定該伺服器。 您將在 [快照集資料夾] 頁面,為「散發者」指定預設快照集資料夾。 快照集資料夾只是您指定為共用的目錄;從這個資料夾讀取與寫入這個資料夾的代理程式必須擁有存取資料夾的足夠權限。 如需有關適當保護資料夾的詳細資訊,請參閱<保護快照集資料夾>。

      如果指定另一台伺服器扮演「散發者」角色,則必須在 [管理密碼] 頁面輸入密碼才能從「發行者」連接到「散發者」。 此密碼必須符合發行者於遠端散發者啟用時所指定的密碼。

      如需詳細資訊,請參閱<設定散發>。

    • 選擇發行集資料庫。

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

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

    • 從資料表發行項中選擇性地篩選資料列。 如需詳細資訊,請參閱<篩選發行的資料>。

    • 設定「快照集代理程式」的排程。

    • 指定下列複寫代理程式執行時使用的認證,並建立連接:

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

      - 所有交易式發行集的記錄讀取器代理程式。

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

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

    • 選擇性地編寫發行集的指令碼。 如需詳細資訊,請參閱<編寫複寫指令碼>。

    • 指定發行集的名稱。

搭配回到頁首連結使用的箭頭圖示[Top]

使用 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 指定 Microsoft SQL Server 登入資訊。 請繼續進行步驟 3。

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

    [!附註]

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

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

  4. 在發行者上,執行 sp_addpublication_snapshot (Transact-SQL)。 針對 @publication 指定步驟 3 中所使用的發行集名稱,以及針對 @snapshot_job_name@password 指定快照集代理程式執行時所使用的 Windows 認證。 如果代理程式會在連接到發行者時使用 SQL Server 驗證,則也必須針對 @publisher_security_mode 指定 0 的值,以及針對 @publisher_login@publisher_password 指定 SQL Server 登入資訊。 這麼做會為發行集建立快照集代理程式作業。

    安全性注意事項安全性注意事項

    當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_login 和 job_password) 都會以純文字的方式傳給散發者。 您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。 如需詳細資訊,請參閱<啟用 Database Engine 的加密連接 (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 指定快照集代理程式執行時所使用的 Windows 認證。 如果代理程式會在連接到發行者時使用 SQL Server 驗證,則也必須針對 @publisher_security_mode 指定 0 的值,以及針對 @publisher_login@publisher_password 指定 SQL Server 登入資訊。 這麼做會為發行集建立快照集代理程式作業。

    安全性注意事項安全性注意事項

    當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_login 和 job_password) 都會以純文字的方式傳給散發者。 您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。 如需詳細資訊,請參閱<啟用 Database Engine 的加密連接 (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'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'110RTM';

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

搭配回到頁首連結使用的箭頭圖示[Top]

使用 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 方法。 如需詳細資訊,請參閱<啟用 Database Engine 的加密連接 (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 方法。 如需詳細資訊,請參閱<啟用 Database Engine 的加密連接 (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

搭配回到頁首連結使用的箭頭圖示[Top]

請參閱

概念

以指令碼變數使用 sqlcmd

發行資料和資料庫物件

Replication Management Objects 概念。

定義發行項

檢視及修改發行集屬性

設定散發

保護散發者

保護發行者