Skip to content

Commit 845902c

Browse files
JavadDavid-Engel
Javad
andauthored
FIX | db_id returns null on Azure SQL Database. (#1294)
* db_id() issue. * closed reader. * commit * review comments Co-authored-by: David Engel <dengel1012@gmail.com>
1 parent e1fcf5b commit 845902c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str
106106
_con.Open();
107107

108108
_cachedServer = _con.DataSource;
109+
bool? dbId = null;
109110

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

128-
if (!(bool)_com.ExecuteScalar())
129+
// db_id() returns the database ID of the current database hence it will always be one line result
130+
using (SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow))
131+
{
132+
if (reader.Read() && reader[0] is not null)
133+
{
134+
dbId = reader.GetBoolean(0);
135+
}
136+
}
137+
138+
if (dbId is null || !dbId.Value)
129139
{
130140
throw SQL.SqlDependencyDatabaseBrokerDisabled();
131141
}

0 commit comments

Comments
 (0)