Export (0) Print
Expand All

How to: Delete a Pull Subscription (RMO Programming)

You can delete pull subscriptions programmatically by using Replication Management Objects (RMO). The RMO classes that you use to delete a pull subscription depend on the type of publication to which the pull subscription is subscribed.

  1. Create connections to both the Subscriber and Publisher by using the ServerConnection Class.

  2. Create an instance of the TransPullSubscription class, and set the PublicationName, DatabaseName, PublisherName, and PublicationDBName properties. Use the Subscriber connection from step 1 to set the ConnectionContext property.

  3. Check the IsExistingObject property to verify that the subscription exists. If the value of this property is false, either the subscription properties in step 2 were defined incorrectly or the subscription does not exist.

  4. Call the Remove method.

  5. Create an instance of the TransPublication class by using the Publisher connection from step 1. Specify Name, DatabaseName and ConnectionContext.

  6. Call the LoadProperties method. If this method returns false, either the properties specified in step 5 are incorrect or the publication does not exist on the server.

  7. Call the RemovePullSubscription method. Specify the name of the Subscriber and the subscription database for the subscriber and subscriberDB parameters.

  1. Create connections to both the Subscriber and Publisher by using the ServerConnection Class.

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

  3. Check the IsExistingObject property to verify that the subscription exists. If the value of this property is false, either the subscription properties in step 2 were defined incorrectly or the subscription does not exist.

  4. Call the Remove method.

  5. Create an instance of the MergePublication class by using the Publisher connection from step 1. Specify Name, DatabaseName and ConnectionContext.

  6. Call the LoadProperties method. If this method returns false, either the properties specified in step 5 are incorrect or the publication does not exist on the server.

  7. Call the RemovePullSubscription method. Specify the name of the Subscriber and the subscription database for the subscriber and subscriberDB parameters.

This example deletes a pull subscription to a transactional publication and removes the subscription registration at the Publisher.

// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";

//Create connections to the Publisher and Subscriber.
ServerConnection subscriberConn = new ServerConnection(subscriberName);
ServerConnection publisherConn = new ServerConnection(publisherName);

// Create the objects that we need.
TransPublication publication;
TransPullSubscription subscription;

try
{
    // Connect to the Subscriber.
    subscriberConn.Connect();

    // Define the pull subscription.
    subscription = new TransPullSubscription();
    subscription.ConnectionContext = subscriberConn;
    subscription.PublisherName = publisherName;
    subscription.PublicationName = publicationName;
    subscription.PublicationDBName = publicationDbName;
    subscription.DatabaseName = subscriptionDbName;

    // Define the publication.
    publication = new TransPublication();
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;
    publication.ConnectionContext = publisherConn;

    // Delete the pull subscription, if it exists.
    if (subscription.IsExistingObject)
    {
        if (publication.LoadProperties())
        {
            // Remove the pull subscription registration at the Publisher.
            publication.RemovePullSubscription(subscriberName, subscriptionDbName);
        }
        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));
        }
        // Delete the pull subscription at the Subscriber.
        subscription.Remove();
    }
    else
    {
        throw new ApplicationException(String.Format(
            "The subscription to {0} does not exist on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement the appropriate error handling here.
    throw new ApplicationException(String.Format(
        "The subscription to {0} could not be deleted.", publicationName), ex);
}
finally
{
    subscriberConn.Disconnect();
    publisherConn.Disconnect();
}

This example deletes a pull subscription to a merge publication and removes the subscription registration at the Publisher.

// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksSalesOrdersMerge";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";

//Create connections to the Publisher and Subscriber.
ServerConnection subscriberConn = new ServerConnection(subscriberName);
ServerConnection publisherConn = new ServerConnection(publisherName);

// Create the objects that we need.
MergePublication publication;
MergePullSubscription subscription;

try
{
    // Connect to the Subscriber.
    subscriberConn.Connect();

    // Define the pull subscription.
    subscription = new MergePullSubscription();
    subscription.ConnectionContext = subscriberConn;
    subscription.PublisherName = publisherName;
    subscription.PublicationName = publicationName;
    subscription.PublicationDBName = publicationDbName;
    subscription.DatabaseName = subscriptionDbName;

    // Define the publication.
    publication = new MergePublication();
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;
    publication.ConnectionContext = publisherConn;

    // Delete the pull subscription, if it exists.
    if (subscription.IsExistingObject)
    {
        // Delete the pull subscription at the Subscriber.
        subscription.Remove();

        if (publication.LoadProperties())
        {
            publication.RemovePullSubscription(subscriberName, subscriptionDbName);
        }
        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));
        }
    }
    else
    {
        throw new ApplicationException(String.Format(
            "The subscription to {0} does not exist on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement the appropriate error handling here.
    throw new ApplicationException(String.Format(
        "The subscription to {0} could not be deleted.", publicationName), ex);
}
finally
{
    subscriberConn.Disconnect();
    publisherConn.Disconnect();
}

Community Additions

ADD
Show:
© 2014 Microsoft