Share via


サブスクリプションの追加

Subscription オブジェクトは、サブスクライバ ID、サブスクリプション データを定義するフィールドのインデクサ、サブスクリプションで通知の生成が有効かどうかなど、サブスクリプション情報を設定するためのプロパティを公開します。

このオブジェクトの Add メソッドは、プロパティに設定されたデータをアプリケーション データベースに書き込み、システムで生成されたサブスクリプションの SubscriptionId を返します。SubscriptionId は、データベースには 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();

定期的なサブスクリプションの追加コード

一部のサブスクリプション クラスでは、定期的なサブスクリプションを使用できます。定期的なサブスクリプションを使用する場合は、実行頻度および開始日付を指定する 2 つの追加のプロパティを指定する必要があります。

定期的なサブスクリプションを作成するには、上記のサンプルの Add メソッドの前に以下に示すコードを追加します。ScheduleRecurrence プロパティは、毎日処理されるサブスクリプションを構成します。ScheduleStart プロパティは、太平洋タイム ゾーンで現在の時刻に処理されるサブスクリプションを設定します。サポートされるタイム ゾーンのコードの一覧については、「タイム ゾーン コード」を参照してください。

ms171385.note(ja-jp,SQL.90).gifメモ :
Subscription クラスの ScheduleStart プロパティと ScheduleRecurrence プロパティの値は、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();

サブスクリプション条件には、多数の AND、OR、または NOT 条件のほか、LinkLeafCondition オブジェクトを介して、さまざまな引数タイプやその他のルールも組み合わせた豊富なオブジェクト グラフを使用できます。

COM 相互運用の例

以下のコード例は、Subscription クラスの SetFieldValue メソッドを使用して、アプリケーション固有のサブスクリプション フィールドの値を設定する方法を示しています。

ms171385.note(ja-jp,SQL.90).gifメモ :
COM 相互運用では、条件ベースのサブスクリプションはサポートされていません。
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."

参照

概念

Subscription オブジェクトの作成
サブスクリプションの更新
サブスクリプションの削除
サブスクリプション フィールド情報の取得
サブスクライバ ロケール一覧の作成
タイム ゾーン一覧の作成

ヘルプおよび情報

SQL Server 2005 の参考資料の入手