如何创建推送订阅(RMO 编程)
您可以使用复制管理对象 (RMO) 以编程的方式创建推送订阅。 用于创建推送订阅的 RMO 类取决于对其创建订阅的发布的类型。
安全说明 |
---|
如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Microsoft Windows .NET Framework 提供的 Cryptographic Services(加密服务)。 |
创建快照或事务发布的推送订阅
使用 ServerConnection 类创建与发布服务器的连接。
使用步骤 1 中的发布服务器连接创建 TransPublication 类的实例。指定 Name、DatabaseName 和 ConnectionContext。
调用 LoadProperties 方法。 如果该方法返回 false,则表示步骤 2 中指定的属性不正确,或者服务器中不存在发布。
在 Attributes 属性和 AllowPush 之间执行逻辑位与(在 Visual C# 中为 &,在 Visual Basic 中为 And)运算。 如果结果为 None,则将 Attributes 设置为 Attributes 和 AllowPush 之间的逻辑位或(在 Visual C# 中为 |,在 Visual Basic 为 Or)的结果。 然后,调用 CommitPropertyChanges 以启用推送订阅。
如果订阅数据库不存在,则使用 Database 类创建该数据库。 有关详细信息,请参阅创建、更改和删除数据库。
创建 TransSubscription 类的实例。
设置下列订阅属性:
在步骤 1 中为 ConnectionContext 创建的与发布服务器的 ServerConnection。
用于 SubscriptionDBName 的订阅数据库的名称。
用于 SubscriberName 的订阅服务器的名称。
用于 DatabaseName 的发布数据库的名称。
用于 PublicationName 的发布的名称。
SynchronizationAgentProcessSecurity 的 Login 和 Password 或 SecurePassword 字段,用于提供分发代理在分发服务器中运行所使用的 Microsoft Windows 帐户的凭据。 该帐户用于与分发服务器进行本地连接,同时还用于使用 Windows 身份验证进行远程连接。
注意 当 sysadmin 固定服务器角色的成员创建订阅时,不需要设置 SynchronizationAgentProcessSecurity,尽管建议这样做。 在这种情况下,代理会模拟 SQL Server Agent 帐户。 有关详细信息,请参阅复制代理安全性模式。
(可选)CreateSyncAgentByDefault 的 true 值(默认值),用于创建用来同步订阅的代理作业。 如果您指定了 false,则只能以编程的方式同步订阅。
(可选)在使用 SQL Server 身份验证连接到订阅服务器时设置 SubscriberSecurity 的 SqlStandardLogin 和 SqlStandardPassword 或 SecureSqlStandardPassword 字段。
调用 Create 方法。
安全说明 在具有远程分发服务器的发布服务器上创建推送订阅时,为所有属性(包括 SynchronizationAgentProcessSecurity)提供的值都将作为纯文本发送到该分发服务器。 在调用 Create 方法之前,应该对发布服务器与其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅加密与 SQL Server 的连接。
创建合并发布的推送订阅
使用 ServerConnection 类创建与发布服务器的连接。
使用步骤 1 中的发布服务器连接创建 MergePublication 类的实例。指定 Name、DatabaseName 和 ConnectionContext。
调用 LoadProperties 方法。 如果该方法返回 false,则表示步骤 2 中指定的属性不正确,或者服务器中不存在发布。
在 Attributes 属性和 AllowPush 之间执行逻辑位与(在 Visual C# 中为 &,在 Visual Basic 中为 And)运算。 如果结果为 None,则将 Attributes 设置为 Attributes 和 AllowPush 之间的逻辑位或(在 Visual C# 中为 |,在 Visual Basic 为 Or)的结果。 然后,调用 CommitPropertyChanges 以启用推送订阅。
如果订阅数据库不存在,则使用 Database 类创建该数据库。 有关详细信息,请参阅创建、更改和删除数据库。
创建 MergeSubscription 类的实例。
设置下列订阅属性:
在步骤 1 中为 ConnectionContext 创建的与发布服务器的 ServerConnection。
用于 SubscriptionDBName 的订阅数据库的名称。
用于 SubscriberName 的订阅服务器的名称。
用于 DatabaseName 的发布数据库的名称。
用于 PublicationName 的发布的名称。
SynchronizationAgentProcessSecurity 的 Login 和 Password 或 SecurePassword 字段,用于提供合并代理在分发服务器中运行时所使用的 Microsoft Windows 帐户的凭据。 该帐户用于与分发服务器进行本地连接,同时还用于使用 Windows 身份验证进行远程连接。
注意 当 sysadmin 固定服务器角色的成员创建订阅时,不需要设置 SynchronizationAgentProcessSecurity,尽管建议这样做。 在这种情况下,代理会模拟 SQL Server Agent 帐户。 有关详细信息,请参阅复制代理安全性模式。
(可选)CreateSyncAgentByDefault 的 true 值(默认值),用于创建用来同步订阅的代理作业。 如果您指定了 false,则只能以编程的方式同步订阅。
(可选)在使用 SQL Server 身份验证连接到订阅服务器时设置 SubscriberSecurity 的 SqlStandardLogin 和 SqlStandardPassword 或 SecureSqlStandardPassword 字段。
(可选)在使用 SQL Server 身份验证连接到发布服务器时设置 PublisherSecurity 的 SqlStandardLogin 和 SqlStandardPassword 或 SecureSqlStandardPassword 字段。
调用 Create 方法。
安全说明 在具有远程分发服务器的发布服务器上创建推送订阅时,为所有属性(包括 SynchronizationAgentProcessSecurity)提供的值都将作为纯文本发送到该分发服务器。 在调用 Create 方法之前,应该对发布服务器与其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅加密与 SQL Server 的连接。
示例
该示例创建事务发布的新推送订阅。 用于运行分发代理作业的 Windows 帐户凭据在运行时通过。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2008R2Replica";
string publicationDbName = "AdventureWorks2008R2";
//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 = "AdventureWorks2008R2Replica"
Dim publicationDbName As String = "AdventureWorks2008R2"
'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 = "AdventureWorks2008R2Replica";
string publicationDbName = "AdventureWorks2008R2";
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 = "AdventureWorks2008R2Replica"
Dim publicationDbName As String = "AdventureWorks2008R2"
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