共用方式為


如何:刪除提取訂閱 (RMO 程式設計)

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

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

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

  2. 建立 TransPullSubscription 類別的執行個體,並設定 PublicationNameDatabaseNamePublisherNamePublicationDBName 屬性。使用步驟 1 中的訂閱者連接,以設定 ConnectionContext 屬性。

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

  4. 呼叫 Remove 方法。

  5. 使用步驟 1 中的發行者連接建立 TransPublication 類別的執行個體。指定 NameDatabaseNameConnectionContext

  6. 呼叫 LoadProperties 方法。如果此方法傳回 false,則表示步驟 5 中指定的屬性不正確,或伺服器上沒有該發行集存在。

  7. 呼叫 RemovePullSubscription 方法。為 subscriber 和 subscriberDB 參數指定訂閱者和訂閱資料庫的名稱。

刪除合併式發行集的提取訂閱

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

  2. 建立 MergePullSubscription 類別的執行個體,並設定 PublicationNameDatabaseNamePublisherNamePublicationDBName 屬性。使用步驟 1 中的連接,以設定 ConnectionContext 屬性。

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

  4. 呼叫 Remove 方法。

  5. 使用步驟 1 中的發行者連接建立 MergePublication 類別的執行個體。指定 NameDatabaseNameConnectionContext

  6. 呼叫 LoadProperties 方法。如果此方法傳回 false,則表示步驟 5 中指定的屬性不正確,或伺服器上沒有該發行集存在。

  7. 呼叫 RemovePullSubscription 方法。為 subscriber 和 subscriberDB 參數指定訂閱者和訂閱資料庫的名稱。

範例

此範例會刪除交易式發行集的提取訂閱,並在發行者上移除訂閱註冊。

           // 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();
            }
' 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 = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"

'Create connections to the Publisher and Subscriber.
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)

' Create the objects that we need.
Dim publication As TransPublication
Dim subscription As TransPullSubscription

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 Then

        If publication.LoadProperties() Then
            ' 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))
        End If
        ' 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
    subscriberConn.Disconnect()
    publisherConn.Disconnect()
End Try

此範例會刪除合併式發行集的提取訂閱,並在發行者上移除訂閱註冊。

         // 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();
            }
' 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 = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"

'Create connections to the Publisher and Subscriber.
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)

' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergePullSubscription

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 Then

        ' Delete the pull subscription at the Subscriber.
        subscription.Remove()

        If publication.LoadProperties() Then
            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))
        End If
    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
    subscriberConn.Disconnect()
    publisherConn.Disconnect()
End Try