다음을 통해 공유


구독 추가

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();

예약 구독에 대한 추가 코드

일부 구독 클래스에서는 예약 구독을 지원합니다. 예약 구독의 경우 일정 되풀이 및 시작 날짜를 제공하기 위한 두 가지 추가 속성을 제공해야 합니다.

위의 예제에서 Add 메서드 앞에 다음 코드를 추가하여 예약 구독을 만듭니다. ScheduleRecurrence 속성은 구독이 매일 처리되도록 구성하고 ScheduleStart 속성은 구독이 태평양 표준시로 현재 시간에 처리되도록 구성합니다. 지원되는 표준 시간대 코드 목록을 보려면 Time Zone Codes를 참조하십시오.

[!참고] Subscription 클래스의 ScheduleStartScheduleRecurrence 속성 값은 Notification Services와 관련된 ICalendar 인터페이스 사양을 따라야 합니다. 자세한 내용은 속성 항목을 참조하십시오.

// 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 메서드를 사용하여 응용 프로그램별 구독 필드 값을 설정하는 방법을 보여 줍니다.

[!참고] 조건 기반 구독에는 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 지원 받기