在查询中使用COUNT_BIG()时的SqlDependency数据洪水

时间:2015-08-10 14:55:21

标签: sql-server websocket sqldependency

修复:代码已更新,现已有效。

尝试为管理仪表板设置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
    { .....}

1 个答案:

答案 0 :(得分:2)

必须检查SqlNotificationEventArgs成员。并非所有通知都指示更新。某些通知(例如您正在获取的通知)表示无效条件或无效查询。您收到无效查询的通知并重新提交,只是为了立即通知相同的原因。广告生厌。

检查通知会指出问题。在您的情况下,问题列在您自己发布的链接的第一个项目符号点中:

  

表格名称必须使用两部分名称进行限定