SqlDependency.Start(connectionString) 每次都返回 false

本文关键字:返回 false Start connectionString SqlDependency | 更新日期: 2023-09-27 18:34:02

>我设置"全部"以访问数据库

use DbName
GRANT SUBSCRIBE QUERY NOTIFICATIONS TO dbuser
use DbName
GRANT SELECT ON OBJECT::schema.tableName TO dbuser
Use DbName
GRANT RECEIVE ON QueryNotificationErrorsQueue TO dbuser
ALTER DATABASE DbName SET TRUSTWORTHY ON
use DbName
alter database DbName SET ENABLE_BROKER

但是当我启动 SqlDependency 时:

bool started = SqlDependency.Start(connectionString);
/

/start 是假的,然后我收到此错误

System.Data 中发生类型为"System.InvalidOperationException"的异常.dll但未在用户代码中处理其他信息:在使用 SqlDependency 而不提供选项值的情况下,必须在执行添加到 SqlDependency 实例的命令之前调用 SqlDependency.Start()。

C# 代码:

private static bool notificationEnabled = false;
private string connString= "Data Source=(local);Initial Catalog=MyDB;UID=dbuser; PWD=pass;";
    public static void EnableNotifications()
    {
        // prevent for calling twice 
        if (notificationEnabled)return; 
        System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connString);
        //startResult is false always
        bool startResult = SqlDependency.Start(connString);
        notificationEnabled = true;
    }

SqlDependency.Start(connectionString) 每次都返回 false

尝试将队列名称添加到"开始"屏幕:

bool startResult = SqlDependency.Start(connString, queueName)

我还必须将服务名称和超时添加到我的 SqlDependency 构造函数中,以使事情正常工作。(它在 VB 中,但你明白了)

dependency = New SqlDependency(command, "Service=" + SERVICE_NAME + ";", Int32.MaxValue)