- //Here we will add a function for register notification (will add sql dependency)
- public void RegisterNotification(DateTime currentTime)
- {
- string conStr = ConfigurationManager.ConnectionStrings["sqlConString"].ConnectionString;
- string sqlCommand = @"SELECT [ContactID],[ContactName],[ContactNo] from [dbo].[Contacts] where [AddedOn] > @AddedOn";
- //you can notice here I have added table name like this [dbo].[Contacts] with [dbo], its mendatory when you use Sql Dependency
- using (SqlConnection con = new SqlConnection(conStr))
- {
- SqlCommand cmd = new SqlCommand(sqlCommand, con);
- cmd.Parameters.AddWithValue("@AddedOn", currentTime);
- if (con.State != System.Data.ConnectionState.Open)
- {
- con.Open();
- }
- cmd.Notification = null;
- SqlDependency sqlDep = new SqlDependency(cmd);
- sqlDep.OnChange += sqlDep_OnChange;
- //we must have to execute the command here
- using (SqlDataReader reader = cmd.ExecuteReader())
- {
- // nothing need to add here now
- }
- }
Loading

Kalyani ShevaleRank #1,111 · 1.7K reputationPosted Oct 24, 2018, 9:28 PM EDT
Gaurav RajRank #2,919 · 493 reputationPosted Oct 24, 2018, 10:29 PM EDT
hello
Laxmidhar SahooRank #383 · 6.6K reputationPosted Oct 25, 2018, 10:49 PM EDT
Madan ShekarRank #428 · 5.9K reputationPosted Oct 26, 2018, 3:09 AM EDT