开发者

How can I check in c# if Broker Service is enabled in SQL?

开发者 https://www.devze.com 2023-01-11 02:08 出处:网络
I wa开发者_开发知识库nt to check if Broker Service is running using code and depending on the status, start sqldependency or not. How can I do that?You can do a simple query:

I wa开发者_开发知识库nt to check if Broker Service is running using code and depending on the status, start sqldependency or not. How can I do that?


You can do a simple query:

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

Alternatively you can just start the SqlDependency and trap the error you get if it hasn't been enabled, but the first method is simpler and better:

  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.
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号