gpt4 book ai didi

c# - 如果在 SQL 中启用了 Broker Service,我如何检查 c#?

转载 作者:太空狗 更新时间:2023-10-29 22:24:42 24 4
gpt4 key购买 nike

我想使用代码检查 Broker Service 是否正在运行,并根据状态启动 sqldependency 或不启动。我该怎么做?

最佳答案

你可以做一个简单的查询:

SELECT is_broker_enabled FROM sys.databases WHERE Name = 'mydatabasename'

或者,您可以只启动 SqlDependency 并捕获您收到的错误(如果它尚未启用),但第一种方法更简单且更好:

  try {
SqlDependency.Start();
} catch (InvalidOperationException ex) {
// If broker hasn't been enabled, you'll get the following exception:
//
// The SQL Server Service Broker for the current database is not enabled, and
// as a result query notifications are not supported. Please enable the Service
// Broker for this database if you wish to use notifications.
}

关于c# - 如果在 SQL 中启用了 Broker Service,我如何检查 c#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3446999/

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