6

我想检查 Broker Service 是否正在使用代码运行,并且根据状态,是否启动 sqldependency。我怎样才能做到这一点?

4

1 回答 1

12

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

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.
  }
于 2010-08-10T08:14:17.677 回答