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

Iam*_*per 5 c# service-broker

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

Ric*_*ard 11

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

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.
  }
Run Code Online (Sandbox Code Playgroud)