添加订阅

Subscription 对象公开允许您设置订阅信息(例如,订阅方 ID、定义订阅数据的字段的索引器,以及是否启用订阅来生成通知)的属性。

此对象的 Add 方法将这些数据写入应用程序数据库,然后返回订阅的系统生成的 SubscriptionIdSubscriptionId 作为 64 位整数存储在数据库中,但作为字符串返回到应用程序。

添加基本订阅

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

// 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

ms171385.note(zh-cn,SQL.90).gif注意:
Subscription 类的 ScheduleStartScheduleRecurrence 属性的值必须符合 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();

添加基于条件的订阅

以下代码显示了如何为使用条件操作的订阅类创建订阅。RuleNameCondition 属性来自 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 示例

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

ms171385.note(zh-cn,SQL.90).gif注意:
对于基于条件的订阅,不支持 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."

请参阅

概念

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

帮助和信息

获取 SQL Server 2005 帮助