|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionQuery Notification in SQL Server 2005 solves the problem of having to maintain a polling database to get the updated data. The new notification service of SQL Server 2005 can perform this amazing task of notifying a .NET code when any DML operations are performed to a specified table. The notification cycle starts from the .NET 2.0 application through the object named SqlCommand cmd = new SqlCommand("SELECT ID, Name, " +
"Address FROM dbo.employee", SQLConnectionObject));
SqlDependency depend = new SqlDependency(cmd);
The dependency has to be started using: SqlDependency.Start(connstring);
//It takes the connection string as a parameter.
Now, the question is how and who would get notified? The answers lies in the SqlDependency depend = new SqlDependency(cmd);
SqlDependency.Start(connstring);
depend.OnChange += new OnChangeEventHandler(MyOnChanged);
So when the notification arrives, it goes to the specified handler. Preparing SQL Server 2005 for Notifications
About NotificationsThe created notification is always executed once. So when a notification arrives, its job is done, and to continue receiving notification, you will have to recreate the The best usage of the query notification is in the case of caching. You don’t have to poll the database from time to time to find out any data changes. You can invalidate the cache in the handler and get the data table again, and along with that re-create the Please note, since the SQL Server 2005 Express does not support the notification service, you will not be able to perform Query Notification in SQL Server 2005 Express. LinksFor more information, you can go through this article: This article covers a custom notification query using SqlNotificationRequest, Service Broker, and Queues. SampleThe following sample code snippet uses the SQL Server notification feature: using System.Data.SqlClient;
public void button1_Click(object sender, EventArgs e)
{
string connstring = @"Data Source=SQLServer2005;" +
@"Initial Catalog=TestDB;Persist Security Info" +
@"=True;User ID=username;Password=password;" +
@"Pooling=True;Min Pool Size=5;Max Pool Size=100;" +
@"Asynchronous Processing=True;" +
@"MultipleActiveResultSets=True;Connect Timeout=15";
List<string> list = new List<string>();
using (SqlConnection conn = new SqlConnection(connstring))
using (SqlCommand cmd = new SqlCommand("SELECT" +
" SELECT ID, Name, Address" +
" FROM dbo.employee", conn))
{
try
{
SqlDependency depend = new SqlDependency(cmd);
SqlDependency.Start(connstring);
depend.OnChange += new
OnChangeEventHandler(MyOnChanged);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
//perform any operation here
while (rdr.Read())
list.Add(rdr[0].ToString());
rdr.Close();
}
catch (Exception ee)
{
string msg = ee.Message;
}
}
}
static void MyOnChanged(object caller, SqlNotificationEventArgs e)
{
//to test place a break point here
//and change any data in the specified table
string msg = "Notified";
}
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||