【问题标题】:SqlDependency is not working event if queues are present and query is valid如果存在队列并且查询有效,则 SqlDependency 不是工作事件
【发布时间】:2014-05-28 08:27:42
【问题描述】:

使用以下代码,不会引发我的 OnChange 事件:

SqlDependency.Start(connectionString);

SqlConnection conn = new SqlConnection(connectionString);
   conn.Open();

                using (SqlCommand command = new SqlCommand(
                    "SELECT Name, Description FROM dbo.Boms",
                    conn))
                {

                    SqlDependency dep = new SqlDependency(command);
                    dep.OnChange += dep_OnChange;

                    // Execute the command.
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        // Process the DataReader.
                    }
                }

我搜索了任何可能的查询限制,但我的查询似乎没问题。 dep 对象也已正确创建,但我尝试(成功)在我的 Boms 表中插入一条记录,但我没有收到任何反馈或事件。 在 Sql Server 2012 中,每次启动应用程序时都会看到一个新队列。

我在系统表中查找了错误,但似乎一切正常。

任何提示至少可以理解问题出在哪里?

谢谢!

【问题讨论】:

  • 执行进程是否有选择权限?
  • @LIUFA:我没有写,我也使用这种返回 true 的方法:private bool CanRequestNotifications() { SqlClientPermission permission = new SqlClientPermission( PermissionState.Unrestricted); try { permission.Demand(); return true; } catch (System.Exception) { return false; } }

标签: c# sql sql-server-2012 sqldependency


【解决方案1】:

阅读The Mysterious NotificationTroubleshooting Query NotificationsTroubleshooting DialogsUsing SQL Trace to Troubleshoot Query Notifications 以获取故障排除提示。

运行此命令以确保作为沙箱上下文执行不会遇到孤立的 dbo sid 问题:

ALTER AUTHORIZATION ON database::[<yourdbname>] TO [sa];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多