可以通过以下方式以编程方式控制复制代理:
在命名空间中使用 Microsoft.SqlServer.Replication 托管代理编程接口。
使用提供的一组参数从命令提示符调用代理可执行文件。
通过命令提示符直接调用复制代理,可以从批处理文件中的命令行脚本以编程方式访问代理。 从命令提示符调用代理时,它会在调用代理或启动批处理文件的用户的 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 ;