sql c# 中的依赖项查询抛出无效 sql,但在 sql server management studio 中有效

sql dependency query in c# throwing invalid sql, but works in sql server management studio

SQL 检查依赖关系的查询在运行时抛出无效,并在 sql 服务器管理工​​作室中给出结果。谁能帮忙看看是什么问题。

我需要 SUM 来显示最终结果

 public void GetAllTicket()
    {
        List<DashboardModel> resultlist = new List<DashboardModel>();

        using (SqlConnection connection = new SqlConnection(_connString))
        {
            string query = "SELECT SUM(CASE WHEN  [AssignedStaffID] = 8 and [IsAssignedStaffOverridden] = 0 THEN 1 ELSE 0 END) myticket,SUM(CASE WHEN [AgentGroupID] = 1 THEN 1 ELSE 0 END) agentgroup,SUM(CASE WHEN [AssignedStaffID] = 0 AND [AgentGroupID] = 1 THEN 1 ELSE 0 END) newticket,SUM(CASE WHEN [AssignedStaffID] = 8 AND [TicketStatusID] = 2 THEN 1 ELSE 0 END) resolvedticket FROM  [dbo].[Ticket]";

            using (SqlCommand command = new SqlCommand(query, connection))
            {
                command.Notification = null;
                SqlDependency dependency = new SqlDependency(command);
                dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                connection.Open();
                DataTable dt = new DataTable();
                SqlDataReader reader = command.ExecuteReader();

                dt.Load(reader);

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        resultlist.Add(new DashboardModel
                            {
                                MyTicketCount = int.Parse(dt.Rows[i]["myticket"].ToString()),
                                AgentGroupTicketCount = int.Parse(dt.Rows[i]["agentgroup"].ToString()),
                                NewTicketCount = int.Parse(dt.Rows[i]["newticket"].ToString()),
                                ResolvedTicketCount = int.Parse(dt.Rows[i]["resolvedticket"].ToString())
                            });
                    }
                }

            }
        }
        NotifyAllClients(resultlist);
    }

我得到 e.info= invalid below

    public void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (e.Info == SqlNotificationInfo.Invalid)
        {
            Console.WriteLine("The above notification query is not valid.");
        }

        if (e.Type == SqlNotificationType.Change)
        {         
            GetAllTicket();
        }

    }

这不是 SqlDependency 的有效查询,因为您使用聚合 (SUM),但没有 group by 子句。有关 SqlDependency 的 Microsoft 文档是此页面: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency%28v=vs.110%29.aspx

上面写着

Query notifications are supported only for SELECT statements that meet a list of specific requirements.

有一个非常有用的页面 link,其中详细列出了所有这些要求,可以在此处找到: https://msdn.microsoft.com/library/ms181122.aspx

"Supported SELECT Statements" 上有一个部分有这个 gem:

The projected columns in the SELECT statement may not contain aggregate expressions unless the statement uses a GROUP BY expression.

可能是您的查询还违反了其他规则,这些规则从代码中看不出来(例如,dbo.tickets 是视图吗?)。