語言

如何:刪除發行集 (RMO 程式設計)

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

移除快照式或交易式發行集

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

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

  3. 設定發行集的 Name 和 DatabaseName 屬性,並將 ConnectionContext 屬性設定為在步驟 1 中建立的連接。

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

  5. 呼叫 Remove 方法。

  6. (選擇性) 如果此資料庫沒有任何其他的交易式發行集存在,則可以針對交易式發行停用此資料庫,如下所示:

    1. 建立 ReplicationDatabase 類別的執行個體。將 ConnectionContext 屬性設定為步驟 1 中 ServerConnection 的執行個體。

    2. 呼叫 LoadProperties 方法。如果此方法傳回 false,請確認此資料庫存在。

    3. 將 EnabledTransPublishing 屬性設為 false。

    4. 呼叫 CommitPropertyChanges 方法。

  7. 關閉連接。

移除合併式發行集

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

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

  3. 設定發行集的 Name 和 DatabaseName 屬性,並將 ConnectionContext 屬性設定為在步驟 1 中建立的連接。

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

  5. 呼叫 Remove 方法。

  6. (選擇性) 如果此資料庫沒有任何其他的合併式發行集存在,則可以針對合併式發行停用此資料庫,如下所示:

    1. 建立 ReplicationDatabase 類別的執行個體。將 ConnectionContext 屬性設定為步驟 1 中 ServerConnection 的執行個體。

    2. 呼叫 LoadProperties 方法。如果此方法傳回 false,請確認此資料庫存在。

    3. 將 EnabledMergePublishing 屬性設為 false。

    4. 呼叫 CommitPropertyChanges 方法。

  7. 關閉連接。

範例

下列範例會刪除交易式發行集。如果此資料庫沒有任何其他的交易式發行集存在,則也會停用交易式發行。

         // Define the Publisher, publication database, 
            // and publication names.
            string publisherName = publisherInstance;
            string publicationName = "AdvWorksProductTran";
            string publicationDbName = "AdventureWorks";

            TransPublication publication;
            ReplicationDatabase publicationDb;

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

            try
            {
                conn.Connect();

                // Set the required properties for the transactional publication.
                publication = new TransPublication();
                publication.ConnectionContext = conn;
                publication.Name = publicationName;
                publication.DatabaseName = publicationDbName;

                // Delete the publication, if it exists and has no subscriptions.
                if (publication.LoadProperties() && !publication.HasSubscription)
                {
                    publication.Remove();
                }
                else
                {
                    // Do something here if the publication does not exist
                    // or has subscriptions.
                    throw new ApplicationException(String.Format(
                        "The publication {0} could not be deleted. " +
                        "Ensure that the publication exists and that all " +
                        "subscriptions have been deleted.",
                        publicationName, publisherName));
                }

                // If no other transactional publications exists,
                // disable publishing on the database.
                publicationDb = new ReplicationDatabase(publicationDbName, conn);
                if (publicationDb.LoadProperties())
                {
                    if (publicationDb.TransPublications.Count == 0)
                    {
                        publicationDb.EnabledTransPublishing = false;
                    }
                }
                else
                {
                    // Do something here if the database does not exist.
                    throw new ApplicationException(String.Format(
                        "The database {0} does not exist on {1}.",
                        publicationDbName, publisherName));
                }
            }
            catch (Exception ex)
            {
                // Implement application error handling here.
                throw new ApplicationException(String.Format(
                    "The publication {0} could not be deleted.",
                    publicationName), ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Define the Publisher, publication database, 
' and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks"

Dim publication As TransPublication
Dim publicationDb As ReplicationDatabase

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

Try
    conn.Connect()

    ' Set the required properties for the transactional publication.
    publication = New TransPublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' Delete the publication, if it exists and has no subscriptions.
    If publication.LoadProperties() And Not publication.HasSubscription Then
        publication.Remove()
    Else
        ' Do something here if the publication does not exist
        ' or has subscriptions.
        Throw New ApplicationException(String.Format( _
         "The publication {0} could not be deleted. " + _
         "Ensure that the publication exists and that all " + _
         "subscriptions have been deleted.", _
         publicationName, publisherName))
    End If

    ' If no other transactional publications exists,
    ' disable publishing on the database.
    publicationDb = New ReplicationDatabase(publicationDbName, conn)
    If publicationDb.LoadProperties() Then
        If publicationDb.TransPublications.Count = 0 Then
            publicationDb.EnabledTransPublishing = False
        End If
    Else
        ' Do something here if the database does not exist.
        Throw New ApplicationException(String.Format( _
         "The database {0} does not exist on {1}.", _
         publicationDbName, publisherName))
    End If
Catch ex As Exception
    ' Implement application error handling here.
    Throw New ApplicationException(String.Format( _
     "The publication {0} could not be deleted.", _
     publicationName), ex)
Finally
    conn.Disconnect()
End Try

下列範例會刪除合併式發行集。如果此資料庫沒有任何其他的合併式發行集存在,則也會停用合併式發行。

         // Define the Publisher, publication database, 
            // and publication names.
            string publisherName = publisherInstance;
            string publicationName = "AdvWorksSalesOrdersMerge";
            string publicationDbName = "AdventureWorks";

            MergePublication publication;
            ReplicationDatabase publicationDb;

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

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

                // Set the required properties for the merge publication.
                publication = new MergePublication();
                publication.ConnectionContext = conn;
                publication.Name = publicationName;
                publication.DatabaseName = publicationDbName;

                // Delete the publication, if it exists and has no subscriptions.
                if (publication.LoadProperties() && !publication.HasSubscription)
                {
                    publication.Remove();
                }
                else
                {
                    // Do something here if the publication does not exist
                    // or has subscriptions.
                    throw new ApplicationException(String.Format(
                        "The publication {0} could not be deleted. " +
                        "Ensure that the publication exists and that all " +
                        "subscriptions have been deleted.",
                        publicationName, publisherName));
                }

                // If no other merge publications exists,
                // disable publishing on the database.
                publicationDb = new ReplicationDatabase(publicationDbName, conn);
                if (publicationDb.LoadProperties())
                {
                    if (publicationDb.MergePublications.Count == 0 && publicationDb.EnabledMergePublishing)
                    {
                        publicationDb.EnabledMergePublishing = false;
                    }
                }
                else
                {
                    // Do something here if the database does not exist.
                    throw new ApplicationException(String.Format(
                        "The database {0} does not exist on {1}.",
                        publicationDbName, publisherName));
                }
            }
            catch (Exception ex)
            {
                // Implement application error handling here.
                throw new ApplicationException(String.Format(
                    "The publication {0} could not be deleted.",
                    publicationName), ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Define the Publisher, publication database, 
' and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks"

Dim publication As MergePublication
Dim publicationDb As ReplicationDatabase

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

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

    ' Set the required properties for the merge publication.
    publication = New MergePublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' Delete the publication, if it exists and has no subscriptions.
    If (publication.LoadProperties() And Not publication.HasSubscription) Then
        publication.Remove()
    Else
        ' Do something here if the publication does not exist
        ' or has subscriptions.
        Throw New ApplicationException(String.Format( _
         "The publication {0} could not be deleted. " + _
         "Ensure that the publication exists and that all " + _
         "subscriptions have been deleted.", _
         publicationName, publisherName))
    End If

    ' If no other merge publications exists,
    ' disable publishing on the database.
    publicationDb = New ReplicationDatabase(publicationDbName, conn)
    If publicationDb.LoadProperties() Then
        If publicationDb.MergePublications.Count = 0 _
        And publicationDb.EnabledMergePublishing Then
            publicationDb.EnabledMergePublishing = False
        End If
    Else
        ' Do something here if the database does not exist.
        Throw New ApplicationException(String.Format( _
         "The database {0} does not exist on {1}.", _
         publicationDbName, publisherName))
    End If
Catch ex As Exception
    ' Implement application error handling here.
    Throw New ApplicationException(String.Format( _
     "The publication {0} could not be deleted.", _
     publicationName), ex)
Finally
    conn.Disconnect()
End Try