Skip to content

SqlConnection doesn't restore database in the new session if connection is lost #4108

@llali

Description

@llali

Describe the bug

this issue doesn't happen all the time but if one connection (on master db) is used to run multiple queries and one of the queries is "Use [db name]", sometimes the query after this is executed in the master database

To reproduce

1- Open SqlConnecion passing master db connection string
2- Create a command from the connection and set the command text from the list below, one by one and run command.ExecuteNonQuery()
CREATE DATABASE [$(DatabaseName)]

USE [$(DatabaseName)]

CREATE Table tb1 (c1 int)

  1. I'm not sure what is causing the connection session to be lost, but what happens is, sometimes we see the application role created in the master database.

Expected behavior

queries after "Use [db name]" should always executed in the db not master even if the connection is on the master database

Further technical details

Microsoft.Data.SqlClient version: (6.1.3)
.NET target: .NET Framework 4.7.2 and .NET 8.0
SQL Server version: (e.g. SQL Server 2022)
Operating system: Windows Server 2022

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Status

In progress

Relationships

None yet

Development

No branches or pull requests

Issue actions