共用方式為


How to: Reinitialize a Subscription (RMO Programming)

Individual subscriptions can be marked for reinitialization so that during the next synchronization, a new snapshot is applied. Subscriptions can be reinitialized programmatically by using Replication Management Objects (RMO). The classes you use depend on the type of publication to which the subscription belongs and the type of subscription (that is, a push or pull subscription).

To reinitialize a pull subscription to a transactional publication

  1. Create a connection to the Subscriber by using the ServerConnection class.

  2. Create an instance of the TransPullSubscription class, and set PublicationName, DatabaseName, PublisherName, PublicationDBName, and the connection from step 1 for ConnectionContext.

  3. Call the LoadProperties method to get the properties of the object.

    ms146923.note(zh-tw,SQL.90).gif附註:
    If this method returns false, either the subscription properties in step 2 were defined incorrectly or the pull subscription does not exist.
  4. Call the Reinitialize method. This method marks the subscription for reinitialization.

  5. Synchronize the pull subscription. For more information, see 如何:同步處理提取訂閱 (RMO 程式設計).

To reinitialize a push subscription to a transactional publication

  1. Create a connection to the Publisher by using the ServerConnection class.

  2. Create an instance of the TransSubscription class, and set PublicationName, DatabaseName, SubscriberName, SubscriptionDBName, and the connection from step 1 for ConnectionContext.

  3. Call the LoadProperties method to get the properties of the object.

    ms146923.note(zh-tw,SQL.90).gif附註:
    If this method returns false, either the subscription properties in step 2 were defined incorrectly or the push subscription does not exist.
  4. Call the Reinitialize method. This method marks the subscription for reinitialization.

  5. Synchronize the push subscription. For more information, see 如何:同步處理發送訂閱 (RMO 程式設計).

To reinitialize a pull subscription to a merge publication

  1. Create a connection to the Subscriber by using the ServerConnection class.

  2. Create an instance of the MergePullSubscription class, and set PublicationName, DatabaseName, PublisherName, PublicationDBName, and the connection from step 1 for ConnectionContext.

  3. Call the LoadProperties method to get the properties of the object.

    ms146923.note(zh-tw,SQL.90).gif附註:
    If this method returns false, either the subscription properties in step 2 were defined incorrectly or the pull subscription does not exist.
  4. Call the Reinitialize method. Pass a value of true to upload changes at the Subscriber before reinitialization or a value of false to reinitialize and lose any pending changes at the Subscriber. This method marks the subscription for reinitialization.

    ms146923.note(zh-tw,SQL.90).gif附註:
    Changes cannot be uploaded if the subscription is expired. For more information, see 合併訂閱已過期且必須上載變更.
  5. Synchronize the pull subscription. For more information, see 如何:同步處理提取訂閱 (RMO 程式設計).

To reinitialize a push subscription to a merge publication

  1. Create a connection to the Publisher by using the ServerConnection class.

  2. Create an instance of the MergeSubscription class, and set PublicationName, DatabaseName, SubscriberName, SubscriptionDBName, and the connection from step 1 for ConnectionContext.

  3. Call the LoadProperties method to get the properties of the object.

    ms146923.note(zh-tw,SQL.90).gif附註:
    If this method returns false, either the subscription properties in step 2 were defined incorrectly or the push subscription does not exist.
  4. Call the Reinitialize method. Pass a value of true to upload changes at the Subscriber before reinitialization or a value of false to reinitialize and lose any pending changes at the Subscriber. This method marks the subscription for reinitialization.

    ms146923.note(zh-tw,SQL.90).gif附註:
    Changes cannot be uploaded if the subscription is expired. For more information, see 合併訂閱已過期且必須上載變更.
  5. Synchronize the push subscription. For more information, see 如何:同步處理發送訂閱 (RMO 程式設計).

範例

This example reinitializes a pull subscription to a transactional publication.

// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksProductTran";
String publicationDbName = "AdventureWorks";
String subscriptionDbName = "AdventureWorksReplica";

// 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 = "AdventureWorks"
Dim subscriptionDbName As String = "AdventureWorksReplica"

' 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

This example reinitializes a pull subscription to a merge publication after first uploading pending changes at the Subscriber.

// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksSalesOrdersMerge";
String publicationDbName = "AdventureWorks";
String subscriptionDbName = "AdventureWorksReplica";

// 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 = "AdventureWorks"
Dim subscriptionDbName As String = "AdventureWorksReplica"

' 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

請參閱

工作

How to: Reinitialize a Subscription (Replication Transact-SQL Programming)

概念

Programming with Replication Management Objects

其他資源

如何:重新初始化 SQL Server Compact Edition 訂閱 (以程式設計的方式)
如何:重新初始化訂閱 (SQL Server Management Studio)
重新初始化訂閱

說明及資訊

取得 SQL Server 2005 協助