新增訂閱
Subscription 物件顯示可讓您設定訂閱資訊的屬性,例如訂閱者識別碼、定義訂閱資料的欄位的索引子,及是否啟用訂閱來產生通知。
這個物件的 Add 方法將該資料寫入至應用程式資料庫,及傳回系統產生的 SubscriptionId 訂閱。SubscriptionId 以 64 位元整數儲存在資料庫中,但會以字串傳回至應用程式。
加入基本訂閱
下列程式碼範例顯示如何使用 Item 方法 (屬於 Subscription 類別) 來設定應用程式特定訂閱欄位的值:
// 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");
// Set the properties that describe the subscription record.
testSubscription.Enabled = true;
testSubscription.SubscriberId = "TestUser1";
// Set the subscription data fields (as defined in the ADF),
// using the indexer to set fields by field name.
testSubscription["DeviceName"] = "Work e-mail";
testSubscription["SubscriberLocale"] = "en-US";
testSubscription["City"] = "Shoreline";
// Add the subscription to the database.
testSubscription.Add();
已排程的訂閱的其他程式碼
有些訂閱類別支援已排程的訂閱。若為已排程的訂閱,您必須提供兩個其他屬性,來提供排程循環和開始日期。
將下列程式碼加入至上述範例的 Add 方法之前,以建立已排程的訂閱。ScheduleRecurrence 屬性設定每日處理訂閱。ScheduleStart 屬性設定訂閱在太平洋時區的目前時間處理。如需支援的時區代碼清單,請參閱<Time Zone Codes>。
附註: |
---|
ScheduleStart 和 ScheduleRecurrence 屬性 (屬於 Subscription 類別) 的值,必須符合 ICalendar 介面規格的 Notification Services 子集。如需詳細資訊,請參閱屬性主題。 |
// Set the recurrence of the subscription.
testSubscription.ScheduleRecurrence = "FREQ=DAILY";
// Set the start date and time of the subscription.
StringBuilder scheduleBuilder = new StringBuilder();
scheduleBuilder.AppendFormat("TZID={0}:{1}{2}{3}T{4}{5}{6}",
"4",
DateTime.Now.Year.ToString("D4"),
DateTime.Now.Month.ToString("D2"),
DateTime.Now.Day.ToString("D2"),
DateTime.Now.Hour.ToString("D2"),
DateTime.Now.Minute.ToString("D2"),
DateTime.Now.Second.ToString("D2"));
testSubscription.ScheduleStart = scheduleBuilder.ToString();
加入條件式訂閱
下列程式碼顯示如何建立一個使用條件動作的訂閱類別之訂閱。RuleName 和 Condition 屬性是來自 Subscription 類別。用來定義條件的物件,例如 OrCondition 類別,是來自 Microsoft.SqlServer.NotificationServices.Rules 命名空間。
這個範例顯示如何使用寫入程式碼的文字值來建立訂閱和條件。訂閱管理介面可提供下拉式清單及文字方塊,讓使用者選取及輸入值。
// Create the NSInstance object.
NSInstance testInstance =
new NSInstance("InventoryTrackerInstance");
// Create the NSApplication object.
NSApplication testApplication =
new NSApplication(testInstance, "InventoryTracker");
// Define subscription properties
Subscription s = new Subscription(testApplication, "InventoryTrackerSubscriptions");
s.SubscriberId = "TestUser1";
s.Enabled = true;
s.RuleName = "InventoryTrackerRule";
s["DeviceName"] = "Work e-mail";
s["SubscriberLocale"] = "en-US";
// Define OrCondition
s.Condition = new OrCondition(
new SimpleLeafCondition(new FieldValue("Quantity"),
SimpleOperator.GreaterThanOrEqualTo,
500),
new SimpleLeafCondition(new FieldValue("Quantity"),
SimpleOperator.LessThanOrEqualTo,
35)
);
// Add subscription
s.Add();
訂閱條件可以是一個豐富的物件圖形,透過 LinkLeafCondition 物件來結合許多 AND、OR 和 NOT 條件,以及不同引數類型及其他規則。
COM Interop 範例
下列程式碼範例顯示如何使用 SetFieldValue 方法 (屬於 Subscription 類別) 來設定應用程式特定訂閱欄位的值:
附註: |
---|
條件式訂閱不支援 COM Interop。 |
Dim testInstance, testApplication, testSubscription, subscriptionId
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 properties that describe the subscription record.
testSubscription.SubscriberId = "TestUser2"
testSubscription.Enabled = true
' Set the subscription data fields
testSubscription.SetFieldValue "DeviceName", "Work e-mail"
testSubscription.SetFieldValue "SubscriberLocale", "en-US"
testSubscription.SetFieldValue "City", "Anaheim"
' Add the subscription to the database.
subscriptionId = testSubscription.Add
wscript.echo "Subscription added."
請參閱
概念
建立訂閱物件
更新訂閱
刪除訂閱
取得訂閱欄位資訊
擴展訂閱者地區設定程式碼
擴展時區清單