如何:刪除發送訂閱 (RMO 程式設計)

您可以使用「複寫管理物件」(RMO) 以程式設計的方式刪除發送訂閱。用於刪除發送訂閱的 RMO 類別取決於該發送訂閱所屬的發行集類型而定。

刪除快照式或交易式發行集的發送訂閱

  1. 使用 ServerConnection 類別建立與訂閱者的連接。

  2. 建立 TransSubscription 類別的執行個體。

  3. 設定 PublicationNameSubscriptionDBNameSubscriberNameDatabaseName 屬性。

  4. 針對 ConnectionContext 屬性設定步驟 1 中的 ServerConnection

  5. 檢查 IsExistingObject 屬性,確認該訂閱存在。如果這個屬性的值為 false,則表示步驟 2 中的訂閱屬性定義錯誤或是此訂閱不存在。

  6. 呼叫 Remove 方法。

刪除合併式發行集的發送訂閱

  1. 使用 ServerConnection 類別建立與訂閱者的連接。

  2. 建立 MergeSubscription 類別的執行個體。

  3. 設定 PublicationNameSubscriptionDBNameSubscriberNameDatabaseName 屬性。

  4. 針對 ConnectionContext 屬性設定步驟 1 中的 ServerConnection

  5. 檢查 IsExistingObject 屬性,確認該訂閱存在。如果這個屬性的值為 false,則表示步驟 2 中的訂閱屬性定義錯誤或是此訂閱不存在。

  6. 呼叫 Remove 方法。

範例

此範例會刪除交易式發行集的發送訂閱。

            // 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(publisherName);

            // Create the objects that we need.
            TransSubscription subscription;

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

                // Define the pull subscription.
                subscription = new TransSubscription();
                subscription.ConnectionContext = conn;
                subscription.SubscriberName = subscriberName;
                subscription.PublicationName = publicationName;
                subscription.SubscriptionDBName = subscriptionDbName;
                subscription.DatabaseName = publicationDbName;

                // Delete the pull subscription, if it exists.
                if (subscription.IsExistingObject)
                {
                    // 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
            {
                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(publisherName)

' Create the objects that we need.
Dim subscription As TransSubscription

Try
    ' Connect to the Subscriber.
    conn.Connect()

    ' Define the pull subscription.
    subscription = New TransSubscription()
    subscription.ConnectionContext = conn
    subscription.SubscriberName = subscriberName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.DatabaseName = publicationDbName

    ' Delete the pull subscription, if it exists.
    If subscription.IsExistingObject Then

        ' 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))
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
        "The subscription to {0} could not be deleted.", publicationName), ex)
Finally
    conn.Disconnect()
End Try

此範例會刪除合併式發行集的發送訂閱。

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

            //Create a connection to the Publisher.
            ServerConnection conn = new ServerConnection(publisherName);

            // Create the objects that we need.
            MergeSubscription subscription;

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

                // Define the pull subscription.
                subscription = new MergeSubscription();
                subscription.ConnectionContext = conn;
                subscription.SubscriberName = subscriberName;
                subscription.PublicationName = publicationName;
                subscription.SubscriptionDBName = subscriptionDbName;
                subscription.DatabaseName = publicationDbName;

                // Delete the pull subscription, if it exists.
                if (subscription.IsExistingObject)
                {
                    // 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
            {
                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"

'Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

' Create the objects that we need.
Dim subscription As MergeSubscription

Try
    ' Connect to the Subscriber.
    conn.Connect()

    ' Define the pull subscription.
    subscription = New MergeSubscription()
    subscription.ConnectionContext = conn
    subscription.SubscriberName = subscriberName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.DatabaseName = publicationDbName

    ' Delete the pull subscription, if it exists.
    If subscription.IsExistingObject Then
        ' 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))
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
        "The subscription to {0} could not be deleted.", publicationName), ex)
Finally
    conn.Disconnect()
End Try