gpt4 book ai didi

c# - SqlDependency.Start(connectionString) 每次返回 false

转载 作者:行者123 更新时间:2023-11-30 20:46:05 28 4
gpt4 key购买 nike

我设置好访问数据库

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com