如何重新初始化订阅(RMO 编程)
单独的订阅可以标记为重新初始化,以便在下一次同步期间应用新的快照。 可以使用复制管理对象 (RMO) 以编程的方式重新初始化订阅。 所用的类取决于订阅所属的发布类型以及订阅类型(即推送订阅或请求订阅)。
重新初始化对事务发布的请求订阅
使用 ServerConnection 类创建与订阅服务器的连接。
创建 TransPullSubscription 类的实例,并设置 PublicationName、DatabaseName、PublisherName、PublicationDBName 以及步骤 1 中 ConnectionContext 的连接。
调用 LoadProperties 方法以获取对象的属性。
注意 如果此方法返回 false,则步骤 2 中对订阅属性的定义不正确,或者请求订阅不存在。
调用 Reinitialize 方法。 此方法将订阅标记为要重新初始化。
同步请求订阅。 有关详细信息,请参阅如何同步请求订阅(RMO 编程)。
重新初始化对事务发布的推送订阅
使用 ServerConnection 类创建与发布服务器的连接。
创建 TransSubscription 类的实例,并设置 PublicationName、DatabaseName、SubscriberName、SubscriptionDBName 以及步骤 1 中 ConnectionContext 的连接。
调用 LoadProperties 方法以获取对象的属性。
注意 如果此方法返回 false,则步骤 2 中对订阅属性的定义不正确,或者推送订阅不存在。
调用 Reinitialize 方法。 此方法将订阅标记为要重新初始化。
同步推送订阅。 有关详细信息,请参阅如何同步推送订阅(RMO 编程)。
重新初始化对合并发布的请求订阅
使用 ServerConnection 类创建与订阅服务器的连接。
创建 MergePullSubscription 类的实例,并设置 PublicationName、DatabaseName、PublisherName、PublicationDBName 以及步骤 1 中 ConnectionContext 的连接。
调用 LoadProperties 方法以获取对象的属性。
注意 如果此方法返回 false,则步骤 2 中对订阅属性的定义不正确,或者请求订阅不存在。
调用 Reinitialize 方法。 传递 true 值以在重新初始化之前上载订阅服务器上的更改,或者传递 false 值以重新初始化并丢失订阅服务器上任何挂起的更改。 此方法将订阅标记为要重新初始化。
注意 如果订阅过期,则无法上载更改。 有关详细信息,请参阅合并订阅已过期,必须上载更改。
同步请求订阅。 有关详细信息,请参阅如何同步请求订阅(RMO 编程)。
重新初始化对合并发布的推送订阅
使用 ServerConnection 类创建与发布服务器的连接。
创建 MergeSubscription 类的实例,并设置 PublicationName、DatabaseName、SubscriberName、SubscriptionDBName 以及步骤 1 中 ConnectionContext 的连接。
调用 LoadProperties 方法以获取对象的属性。
注意 如果此方法返回 false,则步骤 2 中对订阅属性的定义不正确,或者推送订阅不存在。
调用 Reinitialize 方法。 传递 true 值以在重新初始化之前上载订阅服务器上的更改,或者传递 false 值以重新初始化并丢失订阅服务器上任何挂起的更改。 此方法将订阅标记为要重新初始化。
注意 如果订阅过期,则无法上载更改。 有关详细信息,请参阅合并订阅已过期,必须上载更改。
同步推送订阅。 有关详细信息,请参阅如何同步推送订阅(RMO 编程)。
示例
此示例将重新初始化事务发布的请求订阅。
// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksProductTran";
String publicationDbName = "AdventureWorks2008R2";
String subscriptionDbName = "AdventureWorks2008R2Replica";
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
TransPullSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define subscription properties.
subscription = new TransPullSubscription();
subscription.ConnectionContext = conn;
subscription.DatabaseName = subscriptionDbName;
subscription.PublisherName = publisherName;
subscription.PublicationDBName = publicationDbName;
subscription.PublicationName = publicationName;
// If the pull subscription and the job exists, mark the subscription
// for reinitialization and start the agent job.
if (subscription.LoadProperties() && subscription.AgentJobId != null)
{
subscription.Reinitialize();
subscription.SynchronizeWithJob();
}
else
{
// Do something here if the subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exists on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Do appropriate error handling here.
throw new ApplicationException("The subscription could not be reinitialized.", ex);
}
finally
{
conn.Disconnect();
}
' Define server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2008R2"
Dim subscriptionDbName As String = "AdventureWorks2008R2Replica"
' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
Dim subscription As TransPullSubscription
Try
' Connect to the Subscriber.
conn.Connect()
' Define subscription properties.
subscription = New TransPullSubscription()
subscription.ConnectionContext = conn
subscription.DatabaseName = subscriptionDbName
subscription.PublisherName = publisherName
subscription.PublicationDBName = publicationDbName
subscription.PublicationName = publicationName
' If the pull subscription and the job exists, mark the subscription
' for reinitialization and start the agent job.
If subscription.LoadProperties() And (Not subscription.AgentJobId Is Nothing) Then
subscription.Reinitialize()
subscription.SynchronizeWithJob()
Else
' Do something here if the subscription does not exist.
Throw New ApplicationException(String.Format( _
"A subscription to '{0}' does not exists on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Do appropriate error handling here.
Throw New ApplicationException("The subscription could not be reinitialized.", ex)
Finally
conn.Disconnect()
End Try
此示例将在第一次上载订阅服务器上挂起的更改后,重新初始化合并发布的请求订阅。
// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksSalesOrdersMerge";
String publicationDbName = "AdventureWorks2008R2";
String subscriptionDbName = "AdventureWorks2008R2Replica";
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
MergePullSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define subscription properties.
subscription = new MergePullSubscription();
subscription.ConnectionContext = conn;
subscription.DatabaseName = subscriptionDbName;
subscription.PublisherName = publisherName;
subscription.PublicationDBName = publicationDbName;
subscription.PublicationName = publicationName;
// If the pull subscription and the job exists, mark the subscription
// for reinitialization after upload and start the agent job.
if (subscription.LoadProperties() && subscription.AgentJobId != null)
{
subscription.Reinitialize(true);
subscription.SynchronizeWithJob();
}
else
{
// Do something here if the subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exists on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Do appropriate error handling here.
throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
conn.Disconnect();
}
' Define server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2008R2"
Dim subscriptionDbName As String = "AdventureWorks2008R2Replica"
' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
Dim subscription As MergePullSubscription
Try
' Connect to the Subscriber.
conn.Connect()
' Define subscription properties.
subscription = New MergePullSubscription()
subscription.ConnectionContext = conn
subscription.DatabaseName = subscriptionDbName
subscription.PublisherName = publisherName
subscription.PublicationDBName = publicationDbName
subscription.PublicationName = publicationName
' If the pull subscription and the job exists, mark the subscription
' for reinitialization after upload and start the agent job.
If subscription.LoadProperties() And (Not subscription.AgentJobId Is Nothing) Then
subscription.Reinitialize(True)
subscription.SynchronizeWithJob()
Else
' Do something here if the subscription does not exist.
Throw New ApplicationException(String.Format( _
"A subscription to '{0}' does not exists on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Do appropriate error handling here.
Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
conn.Disconnect()
End Try