本文介绍了SqlDependency.Start(connectionString)每次返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将访问数据库"的权限设置为全部"

I set All right to acces the DataBase

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,然后出现此错误

//started is false and then I get this error

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

推荐答案

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

Try adding the queue name to your Start:

bool startResult = SqlDependency.Start(connString, queueName)

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

I also had to add the service name and timeout to my SqlDependency constructor to get things to work right. (its in VB, but you get the idea)

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

这篇关于SqlDependency.Start(connectionString)每次返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 09:16