更新訂閱
使用 Update 方法 (屬於 Subscription 類別) 來修改 Notification Services 應用程式的現有訂閱。您可以利用這兩種方式之一來存取個別訂閱物件:按照訂閱識別碼 (由您建立訂閱時的 Notification Services 決定) ,或者(更可能) 使用 SubscriptionEnumeration 物件來重複訂閱者的訂閱。
下列範例顯示如何使用 Managed 程式碼來更新訂閱,及使用 Microsoft Visual Basic Scripting Edition (VBScript) 來說明 COM Interop。
Managed 程式碼範例
下列範例顯示如何從訂閱列舉取得特定訂閱,然後更新該訂閱。
如果您要開發使用者介面,可從文字和清單方塊中取得值,而不是如下所示的硬式編碼字串。若要讓訂閱者能夠選取訂閱,您可以利用訂閱列舉所傳回的訂閱識別碼來擴展清單方塊。然後您可以讓訂閱者選取訂閱,修改欄位值,然後更新訂閱。
// 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 範例顯示如何使用 SetFieldValue 方法 (屬於 Subscription 類別) 來設定應用程式特定訂閱欄位的值:
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."
請參閱
概念
建立訂閱物件
新增訂閱
刪除訂閱
取得訂閱欄位資訊
擴展訂閱者地區設定程式碼
擴展時區清單