複寫代理程式可以透過下列方式以程序設計方式控制:
在命名空間中使用 Microsoft.SqlServer.Replication Managed Agent程式設計介面。
使用提供的一組參數,從命令提示字元叫用代理程式可執行檔。
直接從命令提示字元叫用復寫代理程式,可讓代理程式以程序設計方式從批處理檔中的命令行腳本存取。 從命令提示字元叫用代理程式時,它會在叫用代理程式或啟動批處理檔之使用者的 Microsoft Windows 安全性帳戶下執行。
您可以使用可執行檔來執行下列複寫代理程式的實例。
叫用複寫代理程式時,您可以使用效能設定檔,將一組定義的參數自動傳遞至代理程式可執行檔。 如需相關資訊,請參閱 Replication Agent Profiles。
範例
下列範例示範如何從命令提示字元叫用複寫代理程式。 您也可以使用複寫管理物件 (RMO) 叫用複寫代理程式。 如需詳細資訊,請參閱同步處理訂閱(複寫)。
備註
已新增這些範例中的換行符,以改善可讀性。 在批處理檔中,命令必須在單行中建立。
執行快照集代理程式
這個範例批處理檔會從命令提示字元叫用快照集代理程式,以產生 AdvWorksSalesOrdersMerge 發行集的快照集。
REM -- Declare variables
SET Publisher=%InstanceName%;
SET PublicationDB=AdventureWorks2012;
SET Publication=AdvWorksSalesOrdersMerge;
REM --Start the Snapshot Agent to generate the snapshot for AdvWorksSalesOrdersMerge.
"C:\Program Files\Microsoft SQL Server\120\COM\SNAPSHOT.EXE" -Publication %Publication%
-Publisher %Publisher% -Distributor %Publisher% -PublisherDB %PublicationDB%
-ReplicationType 2 -OutputVerboseLevel 1 -DistributorSecurityMode 1 ;
執行散發代理程式
這個範例批處理檔會從命令提示字元叫用散發代理程式,以持續將 AdvWorksProductTran 發行集的變更復寫至發送訂閱者。
REM -- Declare the variables.
SET Publisher=%instancename%;
SET Subscriber=%instancename%;
SET PublicationDB=AdventureWorks2012;
SET SubscriptionDB=AdventureWorks2012Replica;
SET Publication=AdvWorksProductsTran;
REM -- Start the Distribution Agent with four subscription streams.
REM -- The following command must be supplied without line breaks.
"C:\Program Files\Microsoft SQL Server\120\COM\DISTRIB.EXE" -Subscriber %Subscriber%
-SubscriberDB %SubscriptionDB% -SubscriberSecurityMode 1 -Publication %Publication%
-Publisher %Publisher% -PublisherDB %PublicationDB% -Distributor %Publisher%
-DistributorSecurityMode 1 -Continuous -SubscriptionType 0 -SubscriptionStreams 4 ;
執行合併代理程式
這個範例批處理檔會從命令提示字元叫用合併代理程式,以同步 處理 AdvWorksSalesOrdersMerge 發行集的提取訂閱。
REM -- Declare the variables.
SET Publisher=%instancename%;
SET Subscriber=%instancename%;
SET PublicationDB=AdventureWorks2012;
SET SubscriptionDB=AdventureWorks2012Replica;
SET Publication=AdvWorksSalesOrdersMerge;
REM --Start the Merge Agent with concurrent upload and download processes.
REM -- The following command must be supplied without line breaks.
"C:\Program Files\Microsoft SQL Server\120\COM\REPLMERG.EXE" -Publication %Publication%
-Publisher %Publisher% -Subscriber %Subscriber% -Distributor %Publisher%
-PublisherDB %PublicationDB% -SubscriberDB %SubscriptionDB% -PublisherSecurityMode 1
-OutputVerboseLevel 2 -SubscriberSecurityMode 1 -SubscriptionType 1 -DistributorSecurityMode 1
-Validate 3 -ParallelUploadDownload 1 ;