SQL 依赖关系未触发

发布于 2024-12-29 07:59:02 字数 2308 浏览 0 评论 0原文

我尝试设置 SQL 依赖项来触发“计数行”查询(用 C#、SQL Server 2008 Express 编写),但在原始订阅 SQLNotificationType 消失后,事件处理程序似乎永远不想再次触发(尽管有行)正在添加,并且我检查了 SQL 并且它返回了预期值...)。

我的代码如下。任何想法都非常感激!

编辑:此代码所在的项目是一个 WPF 程序。我将此特定代码存储在一个单独的类中,我的 WPF 程序在“初始化”事件处理程序中创建该类的实例。然后我在这个类中有一个方法,它基本上首先调用 ConnectToDatabase(),然后调用 SetupSQLDependency()。

编辑2:顺便说一句,这个程序是一个WPF,我希望将其分发给一些用户。目标是每当向数据库添加新行时,WPF 都会更新某些信息。我认为这将是最好的方法,而不是总是查询数据库。

        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
            }
        }

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
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱我好吗 2025-01-05 07:59:02

我相信这里的问题是 COUNT。有关详细信息,请参阅支持的 SELECT 语句的 MSDN 文档

SELECT 语句中的投影列可能不包含聚合表达式,除非该语句使用 GROUP BY 表达式。当提供 GROUP BY 表达式时,选择列表可能包含聚合函数 COUNT_BIG() 或 SUM()。 [...]

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(). [...]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文