本主题介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象(RMO)在 SQL Server 2014 中创建推送订阅。 有关为非 SQL Server 订阅服务器创建推送订阅的信息,请参阅 为非 SQL Server 订阅服务器创建订阅。
使用 SQL Server Management Studio
使用“新建订阅向导”在发布者或订阅者上创建推送订阅。 请按照向导中的各个页面进行操作:
指定出版商和出版。
选择复制代理将运行的位置。 对于推送订阅,请选择《分发代理位置》页面或《合并代理位置》页面上的“运行所有代理程序(推送订阅)”,具体取决于发布的类型。
指定订阅者和订阅数据库。
指定用于复制代理建立连接的登录名和密码:
对于快照发布和事务发布的订阅,请在 “分发代理安全性 ”页上指定凭据。
对于合并发布的订阅,请在“合并代理安全”页上指定凭据。
有关每个代理所需权限的信息,请参阅 R复制代理安全模式。
指定同步计划以及何时应初始化订阅服务器。
为合并出版物指定附加选项:订阅类型;参数化过滤的值。
为允许更新订阅的事务性发布指定其他选项:订阅服务器应立即在发布服务器上提交更改,还是将其写入队列,以及用于订阅服务器连接到发布服务器时使用的凭据。
(可选)编写订阅脚本。
从发布服务器创建推送订阅
在 SQL Server Management Studio 中连接到发布服务器,然后展开该服务器节点。
展开 “复制 ”文件夹,然后展开 “本地发布” 文件夹。
右键单击要为其创建一个或多个订阅的发布,然后单击“ 新建订阅”。
完成“新建订阅向导”中的页面。
从订阅服务器创建推送订阅
在 SQL Server Management Studio 中连接到订阅服务器,然后展开服务器节点。
展开 “复制 ”文件夹。
右键单击 “本地订阅 ”文件夹,然后单击“ 新建订阅”。
在“新建订阅向导”的“发布”页上,从“发布者”下拉列表中选择“<查找 SQL Server 发布者>”或“<查找 Oracle 发布者>”。
在“连接到服务器”对话框中连接到发布者。
在 “发布 ”页上选择一个出版物。
完成“新建订阅向导”中的页面。
使用 Transact-SQL
可以使用复制存储过程以编程方式创建推送订阅。 使用的存储过程将取决于订阅所属的发布类型。
重要
如果可能,请在运行时提示用户输入安全凭据。 如果必须在脚本文件中存储凭据,则必须保护该文件以防止未经授权的访问。
创建一个推送订阅用于快照或事务发布
在发布者的发布数据库中,执行 sp_helppublication,以验证发布是否支持推送订阅。
如果 allow_push 值为 1,则支持推送订阅。
如果allow_push的值为0,则执行sp_changepublication,指定allow_push为@property,
true
为@value。
在发布数据库的发布服务器上,执行 sp_addsubscription。 指定 @publication、 @subscriber 和 @destination_db。 为@subscription_type指定推送值。 要了解如何更新订阅,请参阅 创建事务发布的可更新订阅
在发布者的发布数据库上,执行 sp_addpushsubscription_agent。 指定以下内容:
@subscriber、@subscriber_db和@publication参数。
在分发服务器上,分发代理以@job_login和@job_password的 Microsoft Windows 凭据运行。
注释
使用 Windows 集成身份验证建立的连接始终使用 由@job_login 和 @job_password指定的 Windows 凭据。 分发代理始终使用 Windows 集成身份验证与分发服务器建立本地连接。 默认情况下,代理将使用 Windows 集成身份验证连接到订阅服务器。
(可选)@subscriber_security_mode 的值为 0;SQL Server 的登录信息包括 @subscriber_login 和 @subscriber_password。 如果需要在连接到订阅服务器时使用 SQL Server 身份验证,请指定这些参数。
此订阅分发代理作业的日程安排。 有关详细信息,请参阅 指定同步计划。
重要
使用远程分发器在发布服务器上创建推送订阅时,为所有参数(包括 job_login 和 job_password)提供的值将作为纯文本发送到分发器。 在执行此存储过程之前,应加密发布服务器与其远程分发服务器之间的连接。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)。
创建合并发布的推送订阅
在发布数据库上,通过执行 sp_helpmergepublication来验证发布是否支持推送订阅。
如果 allow_push 值为 1,则发布支持推送订阅。
如果allow_push的值不是1,请执行sp_changemergepublication,并将allow_push指定给@property,
true
指定给@value。
在发布者的发布数据库上,执行 sp_addmergesubscription,并指定以下参数:
@publication。 这是出版物的名称。
@subscriber_type。 对于客户端订阅,请指定 本地 订阅,并为服务器订阅指定 全局订阅。
@subscription_priority。 对于服务器订阅,请指定订阅的优先级(0.00 到 99.99)。
有关详细信息,请参阅 高级合并复制冲突检测和解决。
在发布者的发布数据库上执行 sp_addmergepushsubscription_agent。 指定以下内容:
@subscriber、@subscriber_db和@publication参数。
用于分发服务器上的合并代理运行的 Windows 凭据是 @job_login 和 @job_password。
注释
使用 Windows 集成身份验证建立的连接始终使用 由@job_login 和 @job_password指定的 Windows 凭据。 合并代理始终使用 Windows 集成身份验证与分发服务器建立本地连接。 默认情况下,代理将使用 Windows 集成身份验证连接到订阅服务器。
(可选)@subscriber_security_mode 的值为 0,以及 @subscriber_login 和 @subscriber_password 的 SQL Server 登录信息。 如果需要在连接到订阅服务器时使用 SQL Server 身份验证,请指定这些参数。
(可选)@publisher_security_mode的值为0,以及@publisher_login和@publisher_password的 SQL Server 登录信息。 如果需要在连接到发布服务器时使用 SQL Server 身份验证,请指定这些值。
此订阅的合并代理任务的日程安排。 有关详细信息,请参阅 指定同步计划。
重要
使用远程分发服务器在发布服务器上创建推送订阅时,为所有参数(包括 job_login 和 job_password)提供的值将作为纯文本发送到分发服务器。 在执行此存储过程之前,应加密发布服务器与其远程分发服务器之间的连接。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)。
示例 (Transact-SQL)
以下示例创建事务发布的推送订阅。 使用 sqlcmd 脚本变量在运行时提供登录名和密码值。
-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). 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 @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorks2012Replica';
--Add a push subscription to a transactional publication.
USE [AdventureWorks2012]
EXEC sp_addsubscription
@publication = @publication,
@subscriber = @subscriber,
@destination_db = @subscriptionDB,
@subscription_type = N'push';
--Add an agent job to synchronize the push subscription.
EXEC sp_addpushsubscription_agent
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriptionDB,
@job_login = $(Login),
@job_password = $(Password);
GO
通过以下示例创建针对合并发布的推送订阅。 使用 sqlcmd 脚本变量在运行时提供登录名和密码值。
-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). 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 @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @hostname AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorksReplica';
SET @hostname = N'adventure-works\david8'
-- Add a push subscription to a merge publication.
USE [AdventureWorks2012];
EXEC sp_addmergesubscription
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriptionDB,
@subscription_type = N'push',
@hostname = @hostname;
--Add an agent job to synchronize the push subscription.
EXEC sp_addmergepushsubscription_agent
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriptionDB,
@job_login = $(Login),
@job_password = $(Password);
GO
使用复制管理对象 (RMO)
可以使用复制管理对象(RMO)以编程方式创建推送订阅。 用于创建推送订阅的 RMO 类取决于创建订阅的发布类型。
重要
如果可能,请提示用户在运行时输入安全凭据。 如果必须存储凭据,请使用 Microsoft Windows .NET Framework 提供的 加密服务 。
创建快照或事务发布的推送订阅
使用 ServerConnection 类创建与发布服务器的连接。
使用步骤 1 中的 Publisher 连接创建 TransPublication 类的实例。 指定Name和 DatabaseNameConnectionContext。
调用 LoadProperties 方法。 如果此方法返回
false
,则步骤 2 中指定的属性不正确,或者服务器上不存在发布。在 Visual C# 中的
&
和 Visual Basic 中的And
之间,在 Attributes 属性和 AllowPush 之间执行按位逻辑 AND 操作。 如果结果为None,则将Attributes设置为Attributes和AllowPush之间按位逻辑 OR 运算(在 Visual C# 中为|
,在 Visual Basic 中为Or
)的结果。 然后,调用 CommitPropertyChanges 以启用推送订阅。如果订阅数据库不存在,请使用 Database 该类创建它。 有关详细信息,请参阅 创建、更改和删除数据库。
创建 TransSubscription 类的一个实例。
设置以下订阅属性:
在 ServerConnection 步骤 1 中创建的 ConnectionContext发布服务器。
SubscriptionDBName 的订阅数据库的名称。
订阅者 SubscriberName 的名称。
DatabaseName发布数据库的名称。
发布物名称为PublicationName。
请使用SynchronizationAgentProcessSecurity中的Login、Password或SecurePassword*字段,提供分发代理在分发服务器上运行的Microsoft Windows帐户凭据。 此帐户用于与分发服务器建立本地连接,并使用 Windows 身份验证进行远程连接。
注释
当订阅由固定服务器角色的成员
sysadmin
创建时,不需要设置SynchronizationAgentProcessSecurity,但建议这样做。 在这种情况下,代理将模拟 SQL Server 代理帐户。 有关详细信息,请参阅 复制代理安全模式。(可选)用于CreateSyncAgentByDefault创建用于同步订阅的代理作业的值
true
(默认值)。 如果指定false
,则只能以编程方式同步订阅。(可选) SqlStandardLogin 设置使用 SQL Server 身份验证连接到订阅服务器时的和 SqlStandardPassword 或 SecureSqlStandardPassword 字段 SubscriberSecurity 。
调用 Create 方法。
重要
使用远程分发服务器在发布服务器上创建推送订阅时,为所有属性(包括 SynchronizationAgentProcessSecurity)提供的值以纯文本形式发送到分发服务器。 在调用 Create 该方法之前,应加密发布服务器与其远程分发服务器之间的连接。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)。
创建合并发布的推送订阅
使用 ServerConnection 类创建与发布服务器的连接。
使用步骤 1 中的 Publisher 连接来创建 MergePublication 类的实例。 指定Name和 DatabaseNameConnectionContext。
调用 LoadProperties 方法。 如果此方法返回
false
,则步骤 2 中指定的属性不正确,或者服务器上不存在发布。在属性Attributes和AllowPush之间执行按位逻辑 AND(在 Visual C# 中使用
&
和在 Visual Basic 中使用And
)。 如果结果为None,则将Attributes设置为 Attributes 和 AllowPush 之间执行按位逻辑 OR 运算的结果(在 Visual C# 中为|
,在 Visual Basic 中为Or
)。 然后,调用 CommitPropertyChanges 以启用推送订阅。如果订阅数据库不存在,请使用 Database 该类创建它。 有关详细信息,请参阅 创建、更改和删除数据库。
创建 MergeSubscription 类的一个实例。
设置以下订阅属性:
将 ServerConnection 发送到步骤 1 中创建的 ConnectionContext发布者。
SubscriptionDBName订阅数据库的名称。
订阅者SubscriberName的名称。
DatabaseName发布数据库的名称。
用于PublicationName的出版物名称。
在分发服务器上运行合并代理的 Microsoft Windows 帐户需要通过Login字段、Password字段或SecurePassword*字段来提供凭据。 此帐户用于与分发服务器建立本地连接,并使用 Windows 身份验证进行远程连接。
注释
虽然当订阅由固定服务器角色成员
sysadmin
创建时不要求设置SynchronizationAgentProcessSecurity,但建议这样做。 在这种情况下,代理将模拟 SQL Server 代理帐户。 有关详细信息,请参阅 复制代理安全模式。(可选)设置
true
(默认值)以便通过CreateSyncAgentByDefault创建用于同步订阅的代理作业。 如果指定false
,则只能以编程方式同步订阅。(可选)使用 SQL Server 身份验证连接到订阅服务器时,设置 SubscriberSecurity 的 SqlStandardLogin 和 SqlStandardPassword 或 SecureSqlStandardPassword 字段。
(可选)使用 SQL Server 身份验证连接到发布服务器时,设置 SqlStandardLogin 的 SqlStandardPassword 和 SecureSqlStandardPassword 或 PublisherSecurity 字段。
调用 Create 方法。
重要
在拥有远程分发服务器的发布服务器上创建推送订阅时,所有属性(包括 SynchronizationAgentProcessSecurity)的值都以纯文本形式发送到分发服务器。 在调用 Create 该方法之前,应加密发布服务器与其远程分发服务器之间的连接。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)。
示例 (RMO)
此示例为事务性发布创建新的推送订阅。 用于运行分发代理作业的 Windows 帐户凭据在运行时传递。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
//Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(subscriberName);
// Create the objects that we need.
TransPublication publication;
TransSubscription subscription;
try
{
// Connect to the Publisher.
conn.Connect();
// Ensure that the publication exists and that
// it supports push subscriptions.
publication = new TransPublication();
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
publication.ConnectionContext = conn;
if (publication.IsExistingObject)
{
if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
{
publication.Attributes |= PublicationAttributes.AllowPush;
}
// Define the push subscription.
subscription = new TransSubscription();
subscription.ConnectionContext = conn;
subscription.SubscriberName = subscriberName;
subscription.PublicationName = publicationName;
subscription.DatabaseName = publicationDbName;
subscription.SubscriptionDBName = subscriptionDbName;
// Specify the Windows login credentials for the Distribution Agent job.
subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
subscription.SynchronizationAgentProcessSecurity.Password = winPassword;
// By default, subscriptions to transactional publications are synchronized
// continuously, but in this case we only want to synchronize on demand.
subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand;
// Create the push subscription.
subscription.Create();
}
else
{
// Do something here if the publication does not exist.
throw new ApplicationException(String.Format(
"The publication '{0}' does not exist on {1}.",
publicationName, publisherName));
}
}
catch (Exception ex)
{
// Implement the appropriate error handling here.
throw new ApplicationException(String.Format(
"The subscription to {0} could not be created.", publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksProductTran"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"
'Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
' Create the objects that we need.
Dim publication As TransPublication
Dim subscription As TransSubscription
Try
' Connect to the Publisher.
conn.Connect()
' Ensure that the publication exists and that
' it supports push subscriptions.
publication = New TransPublication()
publication.Name = publicationName
publication.DatabaseName = publicationDbName
publication.ConnectionContext = conn
If publication.IsExistingObject Then
If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
publication.Attributes = publication.Attributes _
Or PublicationAttributes.AllowPush
End If
' Define the push subscription.
subscription = New TransSubscription()
subscription.ConnectionContext = conn
subscription.SubscriberName = subscriberName
subscription.PublicationName = publicationName
subscription.DatabaseName = publicationDbName
subscription.SubscriptionDBName = subscriptionDbName
' Specify the Windows login credentials for the Distribution Agent job.
subscription.SynchronizationAgentProcessSecurity.Login = winLogin
subscription.SynchronizationAgentProcessSecurity.Password = winPassword
' By default, subscriptions to transactional publications are synchronized
' continuously, but in this case we only want to synchronize on demand.
subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand
' Create the push subscription.
subscription.Create()
Else
' Do something here if the publication does not exist.
Throw New ApplicationException(String.Format( _
"The publication '{0}' does not exist on {1}.", _
publicationName, publisherName))
End If
Catch ex As Exception
' Implement the appropriate error handling here.
Throw New ApplicationException(String.Format( _
"The subscription to {0} could not be created.", publicationName), ex)
Finally
conn.Disconnect()
End Try
此示例创建合并发布的新推送订阅。 用于运行合并代理作业的 Windows 帐户凭据在运行时传递。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksSalesOrdersMerge";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
string hostname = @"adventure-works\garrett1";
//Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(subscriberName);
// Create the objects that we need.
MergePublication publication;
MergeSubscription subscription;
try
{
// Connect to the Publisher.
conn.Connect();
// Ensure that the publication exists and that
// it supports push subscriptions.
publication = new MergePublication();
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
publication.ConnectionContext = conn;
if (publication.IsExistingObject)
{
if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
{
publication.Attributes |= PublicationAttributes.AllowPush;
}
// Define the push subscription.
subscription = new MergeSubscription();
subscription.ConnectionContext = conn;
subscription.SubscriberName = subscriberName;
subscription.PublicationName = publicationName;
subscription.DatabaseName = publicationDbName;
subscription.SubscriptionDBName = subscriptionDbName;
subscription.HostName = hostname;
// Set a schedule to synchronize the subscription every 2 hours
// during weekdays from 6am to 10pm.
subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly;
subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32(0x003E);
subscription.AgentSchedule.FrequencyRecurrenceFactor = 1;
subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour;
subscription.AgentSchedule.FrequencySubDayInterval = 2;
subscription.AgentSchedule.ActiveStartDate = 20051108;
subscription.AgentSchedule.ActiveEndDate = 20071231;
subscription.AgentSchedule.ActiveStartTime = 060000;
subscription.AgentSchedule.ActiveEndTime = 100000;
// Specify the Windows login credentials for the Merge Agent job.
subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
subscription.SynchronizationAgentProcessSecurity.Password = winPassword;
// Create the push subscription.
subscription.Create();
}
else
{
// Do something here if the publication does not exist.
throw new ApplicationException(String.Format(
"The publication '{0}' does not exist on {1}.",
publicationName, publisherName));
}
}
catch (Exception ex)
{
// Implement the appropriate error handling here.
throw new ApplicationException(String.Format(
"The subscription to {0} could not be created.", publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"
Dim hostname As String = "adventure-works\garrett1"
'Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergeSubscription
Try
' Connect to the Publisher.
conn.Connect()
' Ensure that the publication exists and that
' it supports push subscriptions.
publication = New MergePublication()
publication.Name = publicationName
publication.DatabaseName = publicationDbName
publication.ConnectionContext = conn
If publication.IsExistingObject Then
If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
publication.Attributes = publication.Attributes _
Or PublicationAttributes.AllowPush
End If
' Define the push subscription.
subscription = New MergeSubscription()
subscription.ConnectionContext = conn
subscription.SubscriberName = subscriberName
subscription.PublicationName = publicationName
subscription.DatabaseName = publicationDbName
subscription.SubscriptionDBName = subscriptionDbName
subscription.HostName = hostname
' Set a schedule to synchronize the subscription every 2 hours
' during weekdays from 6am to 10pm.
subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly
subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32("0x003E", 16)
subscription.AgentSchedule.FrequencyRecurrenceFactor = 1
subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour
subscription.AgentSchedule.FrequencySubDayInterval = 2
subscription.AgentSchedule.ActiveStartDate = 20051108
subscription.AgentSchedule.ActiveEndDate = 20071231
subscription.AgentSchedule.ActiveStartTime = 60000
subscription.AgentSchedule.ActiveEndTime = 100000
' Specify the Windows login credentials for the Merge Agent job.
subscription.SynchronizationAgentProcessSecurity.Login = winLogin
subscription.SynchronizationAgentProcessSecurity.Password = winPassword
' Create the push subscription.
subscription.Create()
Else
' Do something here if the publication does not exist.
Throw New ApplicationException(String.Format( _
"The publication '{0}' does not exist on {1}.", _
publicationName, publisherName))
End If
Catch ex As Exception
' Implement the appropriate error handling here.
Throw New ApplicationException(String.Format( _
"The subscription to {0} could not be created.", publicationName), ex)
Finally
conn.Disconnect()
End Try
另请参阅
查看和修改推送订阅属性
复制安全最佳做法
创建出版物
复制管理对象概念
同步推送订阅
订阅刊物
将 sqlcmd 与脚本变量结合使用