我设置“所有权限”以访问数据库

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);


// started为false,然后出现此错误

System.Data.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理
附加信息:在不提供选项值的情况下使用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;
    }

最佳答案

尝试将队列名称添加到“开始”中:

bool startResult = SqlDependency.Start(connString, queueName)


我还必须将服务名称和超时添加到SqlDependency构造函数中,以使事情正常进行。 (在VB中,但是您知道了)

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

关于c# - SqlDependency.Start(connectionString)每次返回false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27411100/

10-13 07:46