SQLDependency_OnChange-Event fires only one single Time

后端 未结 6 830
故里飘歌
故里飘歌 2020-12-30 21:10

I\'m working with SQLDependency to notify me if there is a change in the Database. After Program Start-Up it works just fine. When I make a first change the Event fires. Wo

相关标签:
6条回答
  • 2020-12-30 21:47

    After changes happened to the database at the first time, you have to execute the command again and re-subscribe to the event.

    The following code is working for me.

    class Program
    {
        static string connectionString = "Server=.;Database=test_sql_dependency;Integrated Security=True;";
    
        static void Main(string[] args)
        {
            // 1. create database
    
            // 2. enable service broker by executing this sql command on the database.
            // alter database test_sql_dependency set enable_broker
    
            // 3. start sql dependency, for some sql server connection string or with queue if you want.
    
            //var queueName = "myFirstQueue";
            //SqlDependency.Start(connectionString, queueName);
            SqlDependency.Start(connectionString);
    
            // complete the rest of the steps in seperate method to be able to call it again when you need to 
            // re-subscribe to the event again, becuase by default it will be executed only one time
    
            RegisterSqlDependency();
    
            Console.WriteLine("Listening to database changes...");
            Console.ReadLine();
        }
    
        static void RegisterSqlDependency()
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
    
                // 4. create a sql command 
                // you can't say select *, and also you have to specefy the db owner (dbo.)
                SqlCommand command = new SqlCommand("select Id, Name from dbo.Employee", connection);
    
                // 5. create dependency and associtate it to the sql command
                SqlDependency dependency = new SqlDependency(command);
    
                // 6. subscribe to sql dependency event
                dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
    
                // 7. execute the command
                using (SqlDataReader reader = command.ExecuteReader())
                {
    
                }
            }
        }
    
        static void OnDependencyChange(object sender, SqlNotificationEventArgs e)
        {
            var InsertOrUpdateOrDelte = e.Info;
    
    
    
            //-----------------------------Finally-------------------------
            // after you knew that there is a change happened 
            // you have to unsubscribe the event and execute the command again and then re-subscribe to the event
    
            // 1. unsubscribe the event
            SqlDependency dependency = sender as SqlDependency;
            dependency.OnChange -= OnDependencyChange;
    
            // 2. re-subscribe to the event and execute the command again
            RegisterSqlDependency();
    
        }
    }
    
    0 讨论(0)
  • 2020-12-30 21:48

    Look my friend:

    dep.OnChange -= this.dep_OnChange;
    

    you un-fired your event; which is not true; just delete this line;

    0 讨论(0)
  • 2020-12-30 21:53

    Not sure if that is your problem but you dispose the command right after you have created it:

    using (SqlCommand cmd = cn.CreateCommand()) 
    {
      ...
      cmd.Dispose(); 
    

    It looks like a bug.

    0 讨论(0)
  • 2020-12-30 21:58

    I was running into this issue as well. You need to create a new SqlDependency entity (after unsubscribing the existing one from the OnChange event) and then run a new ExecuteReader command. I got the idea from this post:

    http://www.codeproject.com/Articles/12335/Using-SqlDependency-for-data-change-events

    This usually makes sense, as once you have been notified of a change you will normally want to re-query the data.

    0 讨论(0)
  • 2020-12-30 21:58

    In your private void dep_OnChange(object sender, SqlNotificationEventArgs e) method after you unsubscribe the dep_OnChange event you should call the private void GetStates() again, to initialize the dep.OnChange event again.

    0 讨论(0)
  • 2020-12-30 22:03

    In GetStates() :

    SqlDependency.Stop(con); SqlDependency.Start(con);

    these lines should be executed only when registering the sql dependency for first time.

    Restrict them when you call the method from OnChange event.

    0 讨论(0)
提交回复
热议问题