【问题标题】:Why doesn't SQL Server Service Broker notify me of an insert to a table?为什么 SQL Server Service Broker 不通知我插入表?
【发布时间】:2013-11-22 17:11:56
【问题描述】:

我已经设置了一个列表,应该知道对其数据源的更改,或多或少如下:

public class SharedList<T>: ObservableCollection<T> where T: XTimeEntity
{
    private const string DependencyQuery = "select TITLE_ACTIVE, TITLE_NAME from TITLE";
    private readonly SqlDependency _dependency = new SqlDependency();

    public SharedList()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["XTime900Context"].ConnectionString;

        SqlDependency.Stop(connectionString);
        using (var sqn = new SqlConnection(connectionString))
        {
            using (var cmd = new SqlCommand(DependencyQuery, sqn))
            {
                _dependency.AddCommandDependency(cmd);
            }
        }
        _dependency.OnChange += DependencyOnOnChange;
        PopulateList();
        SqlDependency.Start(connectionString);
    }
}

然而,当我在 SSMS 中执行 insert TITLE values (1, 1, 'Mr.') 时,没有触发任何事件。触发事件的更改是否必须在 SqlConnection 对象或其他对象上进行?

【问题讨论】:

  • 首先你需要模式名称。选择 .. 从 Schema.TableName。
  • 是的,@Janis,我已经添加了这个并且正在接收通知,但一切都不是很好:[我还有另一个问题] [1]:stackoverflow.com/questions/20234287/…

标签: c# .net ado.net service-broker


【解决方案1】:

我认为您需要执行该命令。阅读官方文档,他们有一个示例。 SqlDependency 非常脆弱。涉及许多使用规则和竞争条件。

【讨论】:

  • 好吧,除非发出cmd.ExecuteQuery之类的东西,否则实际上没有设置通知。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 2023-03-05
  • 2014-03-01
相关资源
最近更新 更多