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 に答える