【问题标题】:SqlDependency.OnChange keep firingSqlDependency.OnChange 不断触发
【发布时间】:2014-02-02 15:01:49
【问题描述】:

据我了解,SqlDependency.OnChange 仅应在查询结果更改时触发 这是一个小应用程序,我在事件中放置了一个计数器,并显示它,即使我没有添加新行,它似乎也会持续触发,我尝试了一些博客中的一些示例,我得到了相同的结果,我是什么做错了吗? 我检查了“sys.transmission_queue”和“sys.dm_qn_subscriptions”都是空的 SqlNotificationEventArgs 属性值为“Info = Invalid,Source = Statement,Type = Subscribe”

    private void Runnn()
    {
        var query = from x in Entities.Contacts select x;
        qqq.ItemsSource = query.ToList();
        con = new SqlConnection(@"server=PC\sqlexpress08;
             database=test2db;Trusted_Connection=yes;");
        command = new SqlCommand("SELECT ID,Name FROM dbo.Contacts",con);
        BeginSqlDependency(con.ConnectionString);
        
    }
    private void BeginSqlDependency(string connection)
    {
        SqlDependency.Stop(connection);
        SqlDependency.Start(connection);
        RegisterSqlDependency();
    }
    private void RegisterSqlDependency()
    {
        
        command.Notification = null;
        dependency = new SqlDependency(command);
        dependency.OnChange += new OnChangeEventHandler(DependencyOnChange);
        RegisterSqlCommand();
    }
    private void RegisterSqlCommand()
    {
        con.Open();
        command.ExecuteNonQuery();
        con.Close();
    }
    private void DependencyOnChange(object sender, SqlNotificationEventArgs e)
    {
        SqlDependency dependency = (SqlDependency)sender;
        dependency.OnChange -= DependencyOnChange;
        var query = from x in Entities.Contacts select x;
        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, _
             new Action(() => { qqq.ItemsSource = null; qqq.ItemsSource = 
             query.ToList(); ee.Text = i.ToString(); }));
        RegisterSqlDependency();
        i++;
    }
    ##SQL server Express SP2 2008##
    ALTER DATABASE test2db SET ENABLE_BROKER; 
    CREATE QUEUE ContactChangeMessages;
    CREATE SERVICE ContactChangeNotifications
    ON QUEUE ContactChangeMessages
    ([http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]);

【问题讨论】:

  • 您在错误的队列中查找。 command.Notification 为 null,因此它使用默认队列名称。 See VB example here

标签: c# sql wpf sqldependency


【解决方案1】:

今天我遇到了完全相同的问题,对我来说,问题与设置选项有关,请注意,您可以通过在 DependencyOnChange 事件处理程序中放置一个断点并查看SqlNotificationEventArgs 信息属性(有关详细信息,请参阅here

为查询通知注册查询需要在其上进行订阅的数据库连接启用正确的 SET OPTIONS:

ANSI_NULLS ON 
ANSI_PADDING ON 
ANSI_WARNINGS ON 
CONCAT_NULL_YIELDS_NULL ON 
QUOTED_IDENTIFIER ON 
NUMERIC_ROUNDABORT OFF 
ARITHABORT ON

然后在 Sql Studio Management 的数据库属性对话框中打开 ARITHABORT(有关详细信息,请参阅 here)。

另外,如果要启用服务代理(Northwind 是数据库名称),请考虑以下步骤

ALTER DATABASE Northwind SET enable_broker WITH ROLLBACK IMMEDIATE
ALTER DATABASE Northwind SET TRUSTWORTHY ON
ALTER AUTHORIZATION ON DATABASE::Northwind TO [sa]

【讨论】:

    【解决方案2】:

    其他会导致 SqlDependency 持续触发的考虑因素:

    1. 使用不特定的查询(即没有通配符“*”或 DISTINCT 用法)。

    2. 未使用完全限定的表名(例如:SELECT Person FROM PersonTable 与 SELECT Person FROM dbo.PersonTable)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 2011-11-02
      • 1970-01-01
      相关资源
      最近更新 更多