简体   繁体   中英

SqlDependency Not firing c#

Hi I am trying to implement Sql Dependency into my windows service code. I have already set my service broker as true. I have written this code inside Windows On Start method. The method OnDependencyChange should start as soon as there is new entry in table. But it never happens. Here is my code,

        System.Data.SqlClient.SqlDependency.Start("connection 
        string;database=dbName;Integrated Security=True;");
        SqlConnection connection = new SqlConnection("connection 
        string;database=dbName;Integrated Security=True;");
        connection.Open();
        using (SqlCommand command = new SqlCommand("SELECT * FROM 
         UserDetails", connection))
        {


            SqlDependency dependency = new SqlDependency(command);



            dependency.OnChange += new
               OnChangeEventHandler(OnDependencyChange);


            using (SqlDataReader reader = command.ExecuteReader())
            {
                // Process the DataReader.
            }
        }



     void OnDependencyChange(object sender, SqlNotificationEventArgs e)
     {
      // Handle the event (for example, invalidate this cache entry).
     }

Please help.

I think you've got a security problem. If I remember correctly, there is a known issue with permissions required for working with SqlDependency. First, you need to verify and, eventually, enable the Service Broker feature. Then, you need to enable query notifications . Only after that, you can enable your SqlDependency events to detect data modifications: Detecting Changes with SqlDependency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM