【问题标题】:Init SqlDependency and notify changes初始化 SqlDependency 并通知更改
【发布时间】:2014-12-23 12:17:52
【问题描述】:

当有人更新数据库中的数据时,我需要告诉我的应用程序。如果我没有误解,SqlDependancy 就是我需要的。我已经关注this tutorial 并编写了这段代码:

class dbListener
{
    public dbListener() 
    {
        Debug.WriteLine(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=12345;");

        SqlDependency.Start(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=12345;");
        connection = new SqlConnection(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=12345;");
        connection.Open();
        SomeMethod();
    }
    SqlConnection connection;
    void SomeMethod()
    {
        // Assume connection is an open SqlConnection.
        // Create a new SqlCommand object.

        //{
            using (SqlCommand command = new SqlCommand("SELECT * FROM dbo.ArchivioErogazioni", connection))
            {
                // Create a dependency and associate it with the SqlCommand.
                SqlDependency dependency = new SqlDependency(command);
                // Maintain the refence in a class member.
                // Subscribe to the SqlDependency event.
                dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
                // Execute the command.
                command.ExecuteReader();
          //  }
        }
    }
    // Handler method
 void OnDependencyChange(object sender, SqlNotificationEventArgs e)
    {
        if (e.Type == SqlNotificationType.Change) 
        { 
            // Handle the event (for example, invalidate this cache entry).
            MessageBox.Show("ikjkjkj");
            Debug.WriteLine("fkldjkfjklgjf");
            SqlDependency dependency = (SqlDependency)sender;
            dependency.OnChange -= OnDependencyChange; 
            SomeMethod();
        }
    }

    void Termination()
    {
        // Release the dependency.
        SqlDependency.Stop(MainWindow.GetConnectionString("Model"));
    }

}

但它不起作用。我的意思是,它运行时没有错误,但是当我尝试测试它,从 SQL Server 2008 Management Studio 更新一些值时,什么也没有发生。我在管理事件的函数中设置了一个断点,但它只在初始化阶段触发。

我做错了吗?我怎样才能达到我的目标?

【问题讨论】:

    标签: sql-server-2008 notifications messaging service-broker sqldependency


    【解决方案1】:

    但它只在初始化阶段触发

    因为您的查询通知订阅无效而触发。您必须检查SqlNotificationEventArgs 成员。只有 Change 类型是更改通知,您可能会得到带有 Statement 源的 Subscribe 类型。您的查询不符合 Creating a Query for Notification 中描述的条件:

    • 语句不能使用星号 (*) 或 table_name.* 语法来指定列。

    【讨论】:

    • 我编辑了查询,非常感谢您提供的链接。如何正确检查通知的类型?我已经编辑了问题,OnDependancyChange 方法。
    • 它似乎有效,但是...仅针对所选列中的前两个更改...为什么?
    • 也许有类似计时器的东西来检查速度的频率?也许我插入的速度太快了?
    • 阅读The Mysterious Notification,它解释了它是如何工作的。并阅读Troubleshooting Query Notifications 以获取故障排除建议。另外,请阅读 SqlDependency.OnChange callback timing 了解您可能遇到的特定场景(调试)。
    • 我几乎已经完成了这个:jgowdy.blogspot.it/2010/01/… 但它也会在初始化时触发事件......为什么?你已经知道这篇文章了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多