Export (0) Print
Expand All

Deleting a Subscription

SQL Server 2005

The Delete method of the Subscription class deletes an existing subscription record in the application database. The examples below show how to delete a subscription using managed code and using Microsoft Visual Basic Scripting Edition (VBScript) to illustrate COM interop.

The following example shows how to locate a specific subscription using a SubscriptionEnumeration object and the GetFieldValue method, and then delete the subscription. This sample will delete all of the subscriber's subscriptions with a City value equal to Edmonds.

If you are developing a user interface, you can use the SubscriptionEnumeration class to list all of the subscriber's subscriptions and then allow the subscriber to select a subscription and then delete it.

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

The following VBScript code example shows how to delete a subscription using COM interop. When using COM interop, you must first create and then initialize objects. The rest of the code is similar to the managed code example above.

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."

Community Additions

ADD
Show:
© 2014 Microsoft