Exforsys

SQL Server 2005 Training

  1. SQL Server 2005 - Configuring Replication
  2. SQL Server 2005 Replication Enhancements
  3. SQL Server 2005 - Mirror Server
  4. SQL Server 2005 - Introduction to Data Availability
  5. SQL Server 2005 - Backing up a Database
  6. SQL Server 2005 - Using Database Snapshots
  7. SQL Server 2005 - Disaster Recovery
  8. SQL Server 2005 - Managing Certificates
  9. SQL Server 2005 - Managing Permissions
  10. Managing SQL Server 2005 Security
  11. SQL Server 2005 - Using the Database Tuning Advisor
  12. SQL Server 2005 - Tuning a Database
  13. Maintain indexes in a SQL Server 2005 database
  14. SQL Server 2005 - Defining Indexes
  15. SQL Server 2005 - Database Backup
  16. SQL Server 2005 - Populating the Database
  17. SQL Server 2005 Configuration Manager
  18. SQL Server 2005 - Using the Sqlcmd Utility
  19. Using the SQL Management Objects
  20. Using SQL Sever Management Studio - Part 2
  21. Using SQL Sever Management Studio - Part 1
  22. SQL Server 2005 - Using Event Notifications
  23. SQL Server 2005 - Using DDL Triggers
  24. SQL Server Monitoring Tools - Server Profiler
  25. SQL Server 2005 - Testing Troubleshooting
  26. SQL Server 2005 - Upgrading from earlier versions of SQL Server
  27. SQL Server 2005 Installation - Maintenance Plan Without Using Wizard
  28. SQL Server 2005 - Unattended Installations
  29. SQL Server 2005 Installation - Maintenance Plan Using Wizard
  30. Installing a Second Copy of SQL Server 2005
  31. Planning to Install SQL Server 2005
  32. SQL Server 2005 Installation
  33. SQL server 2005 Editions
  34. SQL Server 2005 Architecture Overview
  35. SQL Server 2005 - Management studio interface Summary Page
  36. SQL Server 2005 - Server Groups
  37. SQL Server 2005 - Registered Servers
  38. SQL Server 2005 Administrative Tools
  39. Developing Client applications in SQL Server 2005
  40. SQL Server Management Objects
  41. NET CLR in SQL Server 2005
  42. Native HTTP Support in SQL Server 2005
  43. XML Data Types in SQL Server 2005
  44. Using XML in SQL Server 2005
  45. Using Notification Services in SQL Server 2005
  46. SQL Server 2005 - Service Broker
  47. Data Manipulation Language (DML) in SQL Server 2005
  48. T-SQL Enhancements in SQL Server 2005
  49. Security Features in SQL Server 2005 for the Developer
  50. SQL Server Architecture and Components
  51. SQL Server 2005 Management Studio
  52. Overview of SQL Server 2005 for the Database Developer
  53. Getting started with SQL Server 2005

Ads


Home arrow Technical Training arrow SQL Server 2005 Training

Using Notification Services in SQL Server 2005

Author : Exforsys Inc.     Published on: 25th Oct 2005

Using Notification Services in SQL Server 2005

In this tutorial you will learn about using notification services in SQL Server 2005, Notification Services Architecture, Building Notification Services Solutions, Using Notification Services, Subscription Management, Syntax for adding subscribers, Syntax for adding a device, Syntax for Adding the Subscriptions.

Ads

  • Notification Services Architecture
  • Building Notification Services Solutions

This new feature of SQL Server 2005 is directed towards the delivery of event driven or scheduled notifications to users who have opted to receive such notifications. Developers find this feature extremely useful for developing and deploying applications that require notifications to be sent to users based on subscriptions made by them.

The most significant feature of this service is that information can be scheduled to be sent to the user at timed intervals to their computers or mobile devices in response to the occurrence of a predefined event. The event could be the delay of a flight or the reduction of stock below a specified level.

The notification services model is based on the Subscriber/ subscription scenario with some enhancements in that the response to each subscription can be customized. A subscriber is one who has requested for the notification to be sent to him on a specified device or devices. An event is a trigger that shoots out the notification to the subscriber. A notification is the customized message that is shot out to the subscriber when the predefined event has occurred.

The Architecture

The subscriber and subscription information is stored in the SQL Server 2005 database and are defined by two XML files. The instance configuration file contains the information for the notification services instance—including devices, delivery, delivery protocols and channels. All applications hosted by the instance use this information. The definition of the application permitted to use this information is contained in the application definition file. This file contains data specific to the notification application such as the definition of events, subscriptions and notifications.

All subscription information is stored in the SQL Server 2005 database and a managed code interface helps create a custom subscription management application. Event data is collected by event providers and such data is also stored in the database. An active generator inspects events and matches it with the data in the subscription database. Whenever a match is found a notification is generated and T-SQL queries are used to determine the content of the notification. Thereafter the distributor takes over. It formats the notification and dispatches it to the appropriate subscriber via a specified delivery service. Content formatters(such as Extensible Stylesheet language[xslt]) can also be used to format the notifications if required.

All the above components of the notification service are controlled by Microsoft Windows service called NS$InstanceName. The NET STOP and NET START commands can be used to start or stop the service.

Using Notification Services

Notification services have four APIs for programming the service.

1. Subscription management
2. Event provider
3. Content formatting
4. Delivery protocol

The tables, views and stored procedures within the notification services enable the service perform its functions. The .NET subscription management objects interface is implemented to develop custom applications that create and format the notification.

Subscription Management

Subscription management begins with adding a C# directive for Notification services namespace.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Using Microsoft.SqlServer.NotificationServices;
;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Thereafter, the developer has to create a subscriber, add a device for receiving the notifications and then add the subscriptions.

Syntax for adding subscribers is as under:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
public void AddSubscriber(String instanceName, String subscriber) ;;;;;;
{ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//create NSInstance object ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NSInstance testInstance=new NSInstance(instanceName); ;;;;;;;;;;;;;;;;;l
//create subscriber object ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Subscriber mySubscriber=new Subscriber(testInstance); ;;;;;;;;;;;;;;;;;l
//Set Subscriber information ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;l
mySubscriber.SubscriberID= subscriberID; ;;;;;;;;;;;;;;;;;l;;;;;;;;;;;;;
//Add the new subscriber record. ;;;;;;;;;;;;;;;;;l;;;;;;;;;;;;;;;;;;;;;
mySubscriber.Add() ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;llllll
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Syntax for adding a device

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
public void AddEmailSubscriberDevice(String instanceName, string ;;;;;;;
subscriberID, String deviceName, String deviceAddress) ;;;;;;;;;;;;;;;;;
{ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//create NSInstance object ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NSInstance sampleInstance= new NSInstance(instanceName); ;;;;;;;;;;;;;;;
//Create SubscriberDevice object ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SubscriberDevice sampleSubscriberDevice=new SubscriberDevice;;;;;;;;;;;;(sampleInstance); ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Set the subscriber device properties ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sampleSubscriberDevice.SubscriberID=subscriberID; ;;;;;;;;;;;;;;;;;;;;;;
sampleSubscriberDevice.DeviceAddress=deviceAddress; ;;;;;;;;;;;;;;;;;;;;
sampleSubscriberDevice.DeliveryChannelName= “EmailChannel”; ;;;;;;;;;;;;
sampleSubscriberDevice.DeviceTypeName= “Email”; ;;;;;;;;;;;;;;;;;;;;;;;;
sampleSubscriberDevice.DeviceName=deviceName; ;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Add the subscriber device record to the database ;;;;;;;;;;;;;;;;;;;;;
sampleSubscriberDevice.Add();
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Adding the Subscriptions

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Private string addSubscription(String instanceName, ;;;;;;;;;;;;;;;;;;;;
StringApplicationName, String SubscriptionClassName, String ;;;;;;;;;;;;
subscriberID, String deviceName, String City) ;;;;;;;;;;;;;;;;;;;;;;;;;;
{ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Create NSInstance object ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NSInstance sampleInstance=new NSInstance(instanceName); ;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Create NSApplication object ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NSApplication sampleApplication= new NSApplication(sampleInstance, ;;;;;
applicationName); ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Create subscription object ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Subscription samplesubscription= new Subscription(sampleApplication, ;;;
subscriptionClassName); ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sampleApplication.Enabled=true; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
samplesubscription.SubscriberID=subscriberID; ;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Subscription data fields—defined in ADF ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Samplesubscription[“DeviceName”]=deviceName; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
sampleSubscriptin[“SubscriberLocale”]= “xx”; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
sampleSubcription[“City”] =City; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//set the recurrence of the subscription ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Samplesubscription.ScheduleRecurrence= “FREQ-DAILY”; ;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//set the start date and time of the subscription for scheduled ;;;;;;;;
subscriptions only. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
StirngBuilder.AppendFormat(“TZID=20:{0}{1}{2} T080000”, ;;;;;;;;;;;;;;;;
DateTime.Now.Year.ToString(“D4”), ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DateTime.Now.Month.ToString(“D2”), ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Datetime.Now.Day.ToString(“D2”)); ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sampleSubscription.SubscriberID=subscriberID; ;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Add the subscription to the database ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
string subscriptionID=samplesubscription.Add(); ;;;;;;;;;;;;;;;;;;;;;;;;
Return subscriptionID; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Ads

It must be noted that when a notification service is create two XML files are created. These are used to create the initial content, metadata, the instance configuration file and the application definition file.

Custom components can be created for submitting events, formatting notifications and delivery protocols. Three methods of the interface are used for this purpose: Initialize, FormatContent and Close. As the name suggests the Initialize method initializes the notification. Formatcontent actually formats the content and the Close method closes the process.

Robust applications can be created using notification services and custom objects can be implemented for events, delivery or formatting in managed code. This is the break developers were looking for!



 
This tutorial is part of a SQL Server 2005 Training tutorial series. Read it from the beginning and learn yourself.

SQL Server 2005 Training

  1. SQL Server 2005 - Configuring Replication
  2. SQL Server 2005 Replication Enhancements
  3. SQL Server 2005 - Mirror Server
  4. SQL Server 2005 - Introduction to Data Availability
  5. SQL Server 2005 - Backing up a Database
  6. SQL Server 2005 - Using Database Snapshots
  7. SQL Server 2005 - Disaster Recovery
  8. SQL Server 2005 - Managing Certificates
  9. SQL Server 2005 - Managing Permissions
  10. Managing SQL Server 2005 Security
  11. SQL Server 2005 - Using the Database Tuning Advisor
  12. SQL Server 2005 - Tuning a Database
  13. Maintain indexes in a SQL Server 2005 database
  14. SQL Server 2005 - Defining Indexes
  15. SQL Server 2005 - Database Backup
  16. SQL Server 2005 - Populating the Database
  17. SQL Server 2005 Configuration Manager
  18. SQL Server 2005 - Using the Sqlcmd Utility
  19. Using the SQL Management Objects
  20. Using SQL Sever Management Studio - Part 2
  21. Using SQL Sever Management Studio - Part 1
  22. SQL Server 2005 - Using Event Notifications
  23. SQL Server 2005 - Using DDL Triggers
  24. SQL Server Monitoring Tools - Server Profiler
  25. SQL Server 2005 - Testing Troubleshooting
  26. SQL Server 2005 - Upgrading from earlier versions of SQL Server
  27. SQL Server 2005 Installation - Maintenance Plan Without Using Wizard
  28. SQL Server 2005 - Unattended Installations
  29. SQL Server 2005 Installation - Maintenance Plan Using Wizard
  30. Installing a Second Copy of SQL Server 2005
  31. Planning to Install SQL Server 2005
  32. SQL Server 2005 Installation
  33. SQL server 2005 Editions
  34. SQL Server 2005 Architecture Overview
  35. SQL Server 2005 - Management studio interface Summary Page
  36. SQL Server 2005 - Server Groups
  37. SQL Server 2005 - Registered Servers
  38. SQL Server 2005 Administrative Tools
  39. Developing Client applications in SQL Server 2005
  40. SQL Server Management Objects
  41. NET CLR in SQL Server 2005
  42. Native HTTP Support in SQL Server 2005
  43. XML Data Types in SQL Server 2005
  44. Using XML in SQL Server 2005
  45. Using Notification Services in SQL Server 2005
  46. SQL Server 2005 - Service Broker
  47. Data Manipulation Language (DML) in SQL Server 2005
  48. T-SQL Enhancements in SQL Server 2005
  49. Security Features in SQL Server 2005 for the Developer
  50. SQL Server Architecture and Components
  51. SQL Server 2005 Management Studio
  52. Overview of SQL Server 2005 for the Database Developer
  53. Getting started with SQL Server 2005
 

Comments