Skip to content

Commit

Permalink
FIX | db_id returns null on Azure SQL Database. (#1294)
Browse files Browse the repository at this point in the history
* db_id() issue.

* closed reader.

* commit

* review comments

Co-authored-by: David Engel <dengel1012@gmail.com>
  • Loading branch information
Javad and David-Engel authored May 27, 2022
1 parent e1fcf5b commit 845902c
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str
_con.Open();

_cachedServer = _con.DataSource;
bool? dbId = null;

#if NETFRAMEWORK
if (hashHelper.Identity != null)
Expand All @@ -125,7 +126,16 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str
CommandText = "select is_broker_enabled from sys.databases where database_id=db_id()"
};

if (!(bool)_com.ExecuteScalar())
// db_id() returns the database ID of the current database hence it will always be one line result
using (SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow))
{
if (reader.Read() && reader[0] is not null)
{
dbId = reader.GetBoolean(0);
}
}

if (dbId is null || !dbId.Value)
{
throw SQL.SqlDependencyDatabaseBrokerDisabled();
}
Expand Down

0 comments on commit 845902c

Please sign in to comment.