Löschen eines Abonnements

Mit der Delete-Methode der Subscription-Klasse wird ein vorhandener Abonnementdatensatz in der Anwendungsdatenbank gelöscht. In den folgenden Beispielen wird gezeigt, wie ein Abonnement gelöscht wird, indem verwalteter Code und Microsoft Visual Basic Scripting Edition (VBScript) verwendet werden, um COM-Interop zu veranschaulichen.

Beispiel für verwalteten Code

Im folgenden Beispiel wird gezeigt, wie ein bestimmtes Abonnement mithilfe eines SubscriptionEnumeration-Objekts und der GetFieldValue-Methode gesucht und dann gelöscht wird. In diesem Beispiel werden alle Abonnements des Abonnenten mit einem City-Wert von Edmonds gelöscht.

Wenn Sie eine Benutzeroberfläche entwickeln, können Sie die SubscriptionEnumeration-Klasse verwenden, um alle Abonnements des Abonnenten aufzulisten. Dann kann der Abonnent ein Abonnement auswählen und es anschließend löschen.

// Create the NSInstance object.
NSInstance testInstance = new NSInstance("Tutorial");

// Create the NSApplication object.
NSApplication testApplication =
    new NSApplication(testInstance, "Weather");

// Create the Subscription object.
Subscription testSubscription =
    new Subscription(testApplication, "WeatherCity");


// Create a SubscriptionEnumeration object.
// that contains all of the subscriber's subscriptions
// in a specific subscription class
SubscriptionEnumeration testSubscriptionEnumeration =
    new SubscriptionEnumeration(testApplication,
        "WeatherCity", "TestUser1");

// Iterate through the subscriptions, finding the subscription
// that you want to delete.
foreach (Subscription subscription in testSubscriptionEnumeration)
{
    String city = subscription.GetFieldValue("City").ToString();
    if (city.Equals("Edmonds"))
        testSubscription.SubscriptionId = subscription.SubscriptionId;
}

// Delete the subscription
testSubscription.Delete();

COM-Interop-Beispiel

Im folgenden VBScript-Codebeispiel wird gezeigt, wie ein Abonnement mithilfe von COM-Interop gelöscht wird. Wenn Sie COM-Interop verwenden, müssen Sie zunächst Objekte erstellen und diese dann initialisieren. Der Rest des Codes ist dem obigen Beispiel für verwalteten Code ähnlich.

Dim testInstance, testApplication, testSubscriptionEnumeration

const instanceName = "Tutorial"
const applicationName = "Weather"
const subscriptionClassName = "WeatherCity"

' Create the NSInstance object.
set testInstance = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.NSInstance")
testInstance.Initialize instanceName

' Create the NSApplication object.
set testApplication = WScript.CreateObject( _
    "Microsoft.SqlServer.NotificationServices.NSApplication")
testApplication.Initialize (testInstance), applicationName

' Create the SubscriptionEnumeration object.
' This constructor returns the subscriptions for
' the specified application, subscription class, and subscriber.
set testSubscriptionEnumeration = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.SubscriptionEnumeration")
testSubscriptionEnumeration.Initialize (testApplication), _ 
    subscriptionClassName, "TestUser1"

' Step through the subscriptions, locate the one 
' that should be removed, and delete it.
for each thisSubscription in testSubscriptionEnumeration
    if thisSubscription.GetFieldValue("City") = "Orlando" then
        thisSubscription.Delete
    end if
next

wscript.echo "Subscriber deleted."

Siehe auch

Konzepte

Erstellen eines Abonnementobjekts
Hinzufügen eines Abonnements
Aktualisieren eines Abonnements
Abrufen von Abonnementfeldinformationen
Auffüllen einer Abonnentengebietsschema-Liste
Auffüllen einer Zeitzonenliste

Andere Ressourcen

NS<SubscriptionClassName>View

Hilfe und Informationen

Informationsquellen für SQL Server 2005