Come posso controllare in C #, se Service Broker è attivato in SQL?
-
27-09-2019 - |
Domanda
Voglio verificare se Service Broker è in esecuzione utilizzando il codice e in base allo stato, avviare SqlDependency o no. Come posso fare?
Soluzione
Si può fare una semplice query:
SELECT is_broker_enabled FROM sys.databases WHERE Name = 'mydatabasename'
In alternativa si può semplicemente avviare lo SqlDependency e intercettare l'errore che si ottiene se non è stata attivata, ma il primo metodo è più semplice e meglio:
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.
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow