Share via


プッシュ サブスクリプションを削除する方法 (RMO プログラミング)

レプリケーション管理オブジェクト (RMO) を使用することで、プログラムによってプッシュ サブスクリプションを削除できます。プッシュ サブスクリプションの削除に使用する RMO クラスは、プッシュ サブスクリプションをサブスクライブするパブリケーションの種類によって異なります。

スナップショット パブリケーションまたはトランザクション パブリケーションに対するプッシュ サブスクリプションを削除するには

  1. ServerConnection クラスを使用して、サブスクライバへの接続を作成します。

  2. TransSubscription クラスのインスタンスを作成します。

  3. PublicationNameSubscriptionDBNameSubscriberNameDatabaseName の各プロパティを設定します。

  4. 手順 1. の ServerConnectionConnectionContext プロパティに設定します。

  5. IsExistingObject プロパティをチェックして、サブスクリプションが存在することを確認します。このプロパティの値が false の場合、手順 2. でサブスクリプション プロパティが不適切に定義されたか、サブスクリプションが存在していません。

  6. Remove メソッドを呼び出します。

マージ パブリケーションに対するプッシュ サブスクリプションを削除するには

  1. ServerConnection クラスを使用して、サブスクライバへの接続を作成します。

  2. MergeSubscription クラスのインスタンスを作成します。

  3. PublicationNameSubscriptionDBNameSubscriberNameDatabaseName の各プロパティを設定します。

  4. 手順 1. の ServerConnectionConnectionContext プロパティに設定します。

  5. IsExistingObject プロパティをチェックして、サブスクリプションが存在することを確認します。このプロパティの値が false の場合、手順 2. でサブスクリプション プロパティが不適切に定義されたか、サブスクリプションが存在していません。

  6. Remove メソッドを呼び出します。

使用例

次の例では、トランザクション パブリケーションに対するプッシュ サブスクリプションを削除します。

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

//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 = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"

'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 = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";

//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 = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"

'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

参照

処理手順

プッシュ サブスクリプションを削除する方法 (レプリケーション Transact-SQL プログラミング)

その他の技術情報

パブリケーションのサブスクライブ

ヘルプおよび情報

SQL Server 2005 の参考資料の入手