Question

I've tried setting up an SQL Dependency to fire on a "Count Rows" query (written in C#, SQL Server 2008 Express), but after the original subscription SQLNotificationType goes, the event handler never seems to want to fire again (despite rows being added, and I've checked the SQL and it's returning the expected value...).

My code is below. Any thoughts are much appreciated!

EDIT: The project that this code is in is a WPF program. I have this particular code stored in a separate class, which my WPF program creates an instance of in an 'Initialized' event handler. I then have a method in this class that basically calls ConnectToDatabase() first, and then calls SetupSQLDependency().

EDIT 2: As a side note, this program is a WPF which I was hoping to distribute to a few users. The goal was to have the WPF update with certain information whenever new rows were added to a database. I thought that this would be the best way to go about it, as opposed to always querying the database.

        private void ConnectToDatabase()
        {
        //This method is the first to be called, and is the entry 
        // point into my SQL database code.

            databaseConnection = new SqlConnection(connectionString);

            // Setup command used in SqlDependecy 
            SqlCommand tempCmd = new SqlCommand();
            tempCmd.Connection = databaseConnection;
            tempCmd.CommandText = "SELECT COUNT(ID) FROM [Example].[dbo].[ExampleTable]";
            sqlCmd = tempCmd;

            try
            { databaseConnection.Open(); }
            catch (Exception e)
            { writeDebug(e.ToString()); }
        }        

        private void SetupSQLDependency()
        {
            SqlDependency.Stop(connectionString);
            SqlDependency.Start(connectionString);

            sqlCmd.Notification = null;

            // create new dependency for SqlCommand
            SqlDependency sqlDep = new SqlDependency(sqlCmd);
            sqlDep.OnChange += new OnChangeEventHandler(sqlDep_OnChange);

            SqlDataReader reader = sqlCmd.ExecuteReader();
        }

        private void sqlDep_OnChange(object sender, SqlNotificationEventArgs e)
        {
            // FROM: http://msdn.microsoft.com/en-us/a52dhwx7.aspx

            if (e.Type == SqlNotificationType.Change)
            {
            //++++++ THIS IS THE BLOCK THAT IS NEVER TRIGGERED ++++++//
                // Have to remove this as it only work's once
                SqlDependency sqlDep = sender as SqlDependency;
                sqlDep.OnChange -= sqlDep_OnChange;

                // Resetup Dependecy
                SetupSQLDependency();
            }
            else if (e.Type == SqlNotificationType.Subscribe)
            {
                double te = 12; // Used this just to test a break... code is useless
            }
        }
Was it helpful?

Solution

I believe the problem here is the COUNT. Refer to the MSDN documentation for Supported SELECT Statements for more info:

The projected columns in the SELECT statement may not contain aggregate expressions unless the statement uses a GROUP BY expression. When a GROUP BY expression is provided, the select list may contain the aggregate functions COUNT_BIG() or SUM(). [...]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top