我从this MSDN page中提取了一个示例,并几乎逐字使用了该示例。运行时,代码会正确编译,但是changeCount会不断增加,无论返回的数据是否确实发生了变化。当实际发生更改时,dataGridView1可以正确反射(reflect)更改。为什么我的SqlDependency看起来好像是循环触发,即使显然没有任何变化?

来源:

#region Using directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
#endregion

namespace PreAllocation_Check
{
    public partial class Form1 : Form
    {
        int           changeCount = 0;
        const string  tableName = "MoxyPosition";
        const string  statusMessage = "Last: {0} - {1} changes.";
        DataSet       dataToWatch = null;
        SqlConnection MoxyConn = null;
        SqlCommand    SQLComm = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (CanRequestNotifications())
            {
                SqlDependency.Start(GetConnectionString());

                if (MoxyConn == null)
                    MoxyConn = new SqlConnection(GetConnectionString());

                if (SQLComm == null)
                {
                    SQLComm = new SqlCommand(GetSQL(), MoxyConn);

                    SqlParameter prm = new SqlParameter("@Quantity", SqlDbType.Int);
                    prm.Direction = ParameterDirection.Input;
                    prm.DbType = DbType.Int32;
                    prm.Value = 100;
                    SQLComm.Parameters.Add(prm);
                }

                if (dataToWatch == null)
                    dataToWatch = new DataSet();

                GetData();
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            SqlDependency.Stop(GetConnectionString());
            if (MoxyConn != null)
                MoxyConn.Close();
        }

        private bool CanRequestNotifications()
        {
            try
            {
                SqlClientPermission SQLPerm = new SqlClientPermission(PermissionState.Unrestricted);
                SQLPerm.Demand();
                return true;
            }
            catch
            {
                return false;
            }
        }

        private string GetConnectionString()
        {
            return "server=***;database=***;user id=***;password=***";
        }

        private void GetData()
        {
            dataToWatch.Clear();
            SQLComm.Notification = null;
            SqlDependency SQLDep = new SqlDependency(SQLComm);
            SQLDep.OnChange += new OnChangeEventHandler(SQLDep_OnChange);

            using (SqlDataAdapter adapter = new SqlDataAdapter(SQLComm))
            {
                adapter.Fill(dataToWatch, tableName);
                dataGridView1.DataSource = dataToWatch;
                dataGridView1.DataMember = tableName;
            }
        }

        private string GetSQL()
        {
            return "SELECT PortID, CONVERT(money, SUM(PreAllocPos), 1) AS PreAllocation, CONVERT(money, SUM(AllocPos), 1) AS Allocation, CONVERT(money, SUM(PreAllocPos) - SUM(AllocPos), 1) AS PreLessAlloc " +
                   "FROM MoxyPosition " +
                   "WHERE CONVERT(money, PreAllocPos, 1) <> CONVERT(money, AllocPos, 1) " +
                   "GROUP BY PortID " +
                   "ORDER BY PortID ASC;";
        }

        void SQLDep_OnChange(object sender, SqlNotificationEventArgs e)
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;

            if (i.InvokeRequired)
            {
                OnChangeEventHandler tempDelegate = new OnChangeEventHandler(SQLDep_OnChange);
                object[] args = { sender, e };
                i.BeginInvoke(tempDelegate, args);
                return;
            }

            SqlDependency SQLDep = (SqlDependency)sender;
            SQLDep.OnChange -= SQLDep_OnChange;

            changeCount++;
            DateTime LastRefresh = System.DateTime.Now;
            label1.Text = String.Format(statusMessage, LastRefresh.TimeOfDay, changeCount);

            GetData();
        }
    }
}

编辑:值得注意的是,我要针对此数据库运行的数据库当前未启用Broker Service,因此为了测试我的代码,我备份了目标数据库并用新名称还原了它,然后对它运行ALTER DATABASE my_db_name SET ENABLE_BROKER 。我所有的测试都在该备用数据库上进行,这意味着我是该数据库上的唯一用户。

最佳答案

这是一个老问题,但是问题是您的查询不符合要求。

简短答案:
将模式名称添加到表"FROM DBO.MoxyPosition " +
更长的答案:
您会看到list of requirements here,它与创建索引 View 的非常相似。注册SQL依赖项后,如果该依赖项无效,则会立即触发该通知,让您知道它是无效的。当您考虑它时,这是有道理的,因为Visual Studio如何知道SQL引擎的内部要求是什么?

因此,在SQLDep_OnChange函数中,您将要查看依赖项被触发的原因。原因在于e变量(信息,源和类型)。有关事件对象的详细信息可以在这里找到:

  • Info options
  • Source
  • Type

  • 对于您的特定情况,请注意Type属性如何MS describes:
    Gets a value that indicates whether this notification is generated
    because of an actual change, OR BY THE SUBSCRIPTION.
    

    关于c# - 使用SqlDependency会导致不断更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5980448/

    10-13 08:00