更新订阅

使用 Subscription 类的 Update 方法可修改 Notification Services 应用程序的现有订阅。可以按订阅 ID 访问某个订阅对象,订阅 ID 是创建订阅时由 Notification Services 决定的,或者,更为可能的是,通过使用 SubscriptionEnumeration 对象循环访问订阅方的订阅来访问某个订阅对象。

下面的示例显示了如何使用托管代码和使用 Microsoft Visual Basic Scripting Edition (VBScript) 更新订阅,以便用示例说明 COM interop。

托管代码示例

以下示例显示了如何从订阅枚举获取特定订阅,然后更新该订阅。

如果正在开发用户界面,可以从文本框和列表框中获取值,而不是如下所示的硬编码字符串。若允许订阅方选择订阅,您可以使用从订阅枚举返回的订阅标识符填充列表框。然后,将允许订阅方选择订阅,修改字段值,再更新订阅。

// 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
// to match. In a user interface, you would probably list the 
// subscriptions in a list box and then let the user
// alter the data for the selected subscription
foreach (Subscription subscription in testSubscriptionEnumeration)
{
    String city = subscription.GetFieldValue("City").ToString();
    Console.WriteLine(city);
    Console.WriteLine("Subscription ID: {0}", subscription.SubscriptionId);
        if (city.Equals("Shoreline"))
            testSubscription.SubscriptionId = subscription.SubscriptionId;
}

// Set all of the subscription data fields 
testSubscription["DeviceName"] = "Work e-mail";
testSubscription["SubscriberLocale"] = "en-US";
testSubscription["City"] = "Edmonds";

testSubscription.Update();

COM Interop 示例

以下 VBScript 示例显示如何使用 Subscription 类的 SetFieldValue 方法来设置应用程序特定的订阅字段的值:

Dim testInstance, testApplication, testSubscription

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 Subscription object.
set testSubscription = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.Subscription")
testSubscription.Initialize (testApplication), subscriptionClassName

' Set the subscription ID so that 
'the correct record is updated.
testSubscription.SubscriptionId = "2"

' Update the subscription record.
testSubscription.SetFieldValue "DeviceName", "Work e-mail"
testSubscription.SetFieldValue "SubscriberLocale", "en-US"
testSubscription.SetFieldValue "City", "Orlando"
testSubscription.Update

wscript.echo "Subscription updated."

请参阅

概念

创建订阅对象
添加订阅
删除订阅
获取订阅字段信息
填充订阅服务器区域设置列表
填充时区列表

其他资源

NS<SubscriptionClassName>View

帮助和信息

获取 SQL Server 2005 帮助