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(fr-fr,SQL.90).gifRemarque :
    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 Procédure : synchroniser un abonnement par extraction de données (pull) (Programmation 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(fr-fr,SQL.90).gifRemarque :
    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 Procédure : pour synchroniser un abonnement d'envoi de données (programmation 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(fr-fr,SQL.90).gifRemarque :
    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(fr-fr,SQL.90).gifRemarque :
    Changes cannot be uploaded if the subscription is expired. For more information, see Un abonnement de fusion a expiré et des modifications doivent être chargées.
  5. Synchronize the pull subscription. For more information, see Procédure : synchroniser un abonnement par extraction de données (pull) (Programmation 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(fr-fr,SQL.90).gifRemarque :
    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(fr-fr,SQL.90).gifRemarque :
    Changes cannot be uploaded if the subscription is expired. For more information, see Un abonnement de fusion a expiré et des modifications doivent être chargées.
  5. Synchronize the push subscription. For more information, see Procédure : pour synchroniser un abonnement d'envoi de données (programmation RMO).

Exemple

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

Voir aussi

Tâches

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

Concepts

Programming with Replication Management Objects

Autres ressources

Procédure : réinitialiser un abonnement SQL Server Compact Edition (par programme)
Procédure : réinitialiser un abonnement (SQL Server Management Studio)
Réinitialisation d'un abonnement

Aide et Informations

Assistance sur SQL Server 2005