【问题标题】:SqlDependency Data Flood when using COUNT_BIG() in query在查询中使用 COUNT_BIG() 时的 SqlDependency 数据泛滥
【发布时间】:2015-08-11 03:02:15
【问题描述】:

已修复:代码已更新,现在可以使用。

尝试为管理仪表板设置 websocket,我需要使用 count_big() 字段和 GROUP BY 子句进行查询。标准记录集列表效果很好,但是一旦我添加了 count_big(),websocket 就不会停止发送数据。我有 read this post about limitations 并且 count_big() 似乎可以使用。 TIA

    using Microsoft.Web.WebSockets;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web;
    using System.Web.Http;

    namespace DatabaseChangeNotification.Controllers

{
    public class DatabaseNotificationController : ApiController
    {
        public HttpResponseMessage Get()
        {
            HttpContext.Current.AcceptWebSocketRequest(new ChatWebSocketHandler());
            return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
        }

        class ChatWebSocketHandler : Microsoft.Web.WebSockets.WebSocketHandler
        {

            public string wsData = null;
            public SqlCommand gblCommand = null;

            public ChatWebSocketHandler()
            {
                SetupNotifier();
            }

            protected void SetupNotifier()
            {
                 using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
                {
                    connection.Open();
                    // DO NOT USE any "*" in queries
                    // WHen using count the variable was converted to string.  Got Data flood
                    // 
                    // Testing count_big data type
                    //  getString failed
                    //
                     using (SqlCommand command = new SqlCommand(@"select [address], count_big(*) as [CurrentTotal] from dbo.users where address = 'main st' group by address", connection)) 
                    {
                        command.Notification = null;
                        SqlDependency dependency = new SqlDependency(command);
                        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                        if (connection.State == ConnectionState.Closed)
                        {
                            connection.Open();
                        }
                        //SqlCommand gblCommand = command;
                        wsData = null;
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {

                                /* MUST MATCH column count and column data type */

                                // wsData += reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetString(2);

                                /* THIS WORKS FOR GETTING NUMERIC VARIABLES */
                                 wsData += reader.GetValue(0) + " " + int.Parse(reader.GetValue(1).ToString());

                                // wsData += reader.GetString(0) + "</br>";  //works but we get data flood and no numbers


                            }
                            // reader.Close();
                        }
                        _chatClients.Broadcast("data: " + wsData);

                    }
                }
            } //SetupNotifier

            private static WebSocketCollection _chatClients = new WebSocketCollection();

            public override void OnOpen()
            {
                _chatClients.Add(this);
            } // OnOpen

            public override void OnMessage(string msg)
            {
             } // OnMessage

            private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
            {

             if (e.Type != SqlNotificationType.Change)
            {
                _chatClients.Broadcast("Returning, not a change notification ");
                return;
            }

               /*
                * Must remove dependency. Only works once.
                */
                SqlDependency dependency = sender as SqlDependency;
                dependency.OnChange -= dependency_OnChange;

                 //reset for next message.
                 SetupNotifier();

            } // dependency_OnChange
        } // ChatWebSocketHandler

    } // DatabaseNotificationController
}

注意:这是在修复代码之前发生的。

Web Socket returns infinite listing:
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    data: main st 1
    { .....}

【问题讨论】:

    标签: sql-server websocket sqldependency


    【解决方案1】:

    必须检查SqlNotificationEventArgs 成员。并非所有通知都表示更新。某些通知(例如您收到的通知)指示无效条件或无效查询。您收到无效查询的通知并重新提交,只是出于同样的原因立即收到通知。恶心。

    检查通知将指向问题所在。在您的情况下,问题列在您自己发布的链接的第一个要点中:

    表名必须由两部分名称限定

    【讨论】:

    • SqlNotificationEventArgs: info: Invalid source: Statement 有什么想法有什么问题吗?我将表名更改为 dbo.users 并没有帮助。 select dbo.users.address, count_big(dbo.users.address) as [CurrentTotal] from dbo.users where dbo.users.address = 'main st' group by dbo.users.address
    • 已修复:Remus,感谢您为我指明了正确的方向。最后的查询是: select [address], count_big() as [CurrentTotal] from dbo.users where [address] = 'main st' group by [address] 注意将 count_big() 更改为count_big(*) 和 GODB.DBO.Users 到 DBO.users。还添加了 if (e.Type != SqlNotificationType.Change) { _chatClients.Broadcast("Returning, not a change notification");返回; } 到 dependency_OnChange() 函数
    猜你喜欢
    • 2012-06-17
    • 2016-05-13
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 2014-10-30
    相关资源
    最近更新 更多