Skip to content

Commit 64d3c8c

Browse files
authored
Migrate System.Data.SqlClient to Microsoft.Data.SqlClient (Batch 4) (#36888)
* docs: Migrate System.Data.SqlClient to Microsoft.Data.SqlClient (Batch 4) Update 5 files: 2 VB FILESTREAM codesnippets, 2 XML/ASP.NET articles, 1 error reference article. Includes ms.date refresh, heading case fixes, and See Also to Related content standardization. * docs: Add ai-usage metadata tag to edited articles
1 parent b8bed13 commit 64d3c8c

5 files changed

Lines changed: 62 additions & 56 deletions

File tree

docs/relational-databases/blob/codesnippet/visualbasic/access-filestream-data-w_0_2.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Imports System
33
Imports System.Collections.Generic
44
Imports System.Text
55
Imports System.Data
6-
Imports System.Data.SqlClient
7-
Imports System.Data.SqlTypes
6+
Imports Microsoft.Data.SqlClient
7+
Imports Microsoft.Data.SqlTypes
88

99
Module Module1
1010
Public Sub Main(ByVal args As String())

docs/relational-databases/blob/codesnippet/visualbasic/create-client-applicatio_4.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Imports System
33
Imports System.Collections.Generic
44
Imports System.Text
55
Imports System.Data
6-
Imports System.Data.SqlClient
7-
Imports System.Data.SqlTypes
6+
Imports Microsoft.Data.SqlClient
7+
Imports Microsoft.Data.SqlTypes
88

99
Module Module1
1010
Public Sub Main(ByVal args As String())

docs/relational-databases/errors-events/mssqlserver-4064-database-engine-error.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ description: "MSSQLSERVER_4064"
44
author: MashaMSFT
55
ms.author: mathoma
66
ms.reviewer: sureshka, randolphwest
7-
ms.date: 01/10/2024
7+
ms.date: 03/17/2026
8+
ai-usage: ai-assisted
89
ms.service: sql
910
ms.subservice: supportability
1011
ms.topic: "reference"
@@ -37,19 +38,19 @@ Permission issues can be one or more of the following:
3738
- You assigned a default database to the login but didn't create a user mapping in the specified database.
3839
- The mapped user for the login has been denied access. (For example, this can happen if the user is inadvertently added to a **db_denydatareader** fixed database role.)
3940

40-
The default database may be unavailable at the time of connection for the following reasons:
41+
The default database might be unavailable at the time of connection for the following reasons:
4142

4243
- The default database is in suspect mode.
4344
- The default database no longer exists.
44-
- The default database name is not correct.
45+
- The default database name isn't correct.
4546
- The default database is in single-user mode, and the only available connection is already used by someone or something else.
4647
- The default database has been detached.
4748
- The default database is set to a RESTRICTED_USER state.
4849
- The default database is offline.
4950
- The default database is set to an EMERGENCY state.
5051
- The default database is part of a database mirror.
5152

52-
Additionally, the login account may be a member of multiple groups, and the default database for one of those groups is unavailable at the time of connection.
53+
Additionally, the login account might be a member of multiple groups, and the default database for one of those groups is unavailable at the time of connection.
5354

5455
For more information about database users in SQL Server, see [Create a Database User](../security/authentication-access/create-a-database-user.md).
5556

@@ -59,7 +60,7 @@ You can take one of the following actions:
5960

6061
### Work around the issue
6162

62-
If you don't need to access the currently configured default database and you just need to connect to the SQL Server instance for other operations using SQL Server Management Studio (SSMS), follow these steps:
63+
If you don't need to access the currently configured default database and you just need to connect to the SQL Server instance for other operations by using SQL Server Management Studio (SSMS), follow these steps:
6364

6465
1. Open SQL Server Management Studio (SSMS).
6566

@@ -73,7 +74,7 @@ If you don't need to access the currently configured default database and you ju
7374

7475
- If the login is a member of the sysadmin role, enter `master`, and select **Connect** to establish a connection to SQL Server. Once you're successfully connected to SQL Server, you can change your default database to a different one that's currently available in the **General** page of login's properties in SSMS. For more information, see [Create a Login](../security/authentication-access/create-a-login.md).
7576

76-
- If the login isn't a member of the sysadmin role, enter a database name on the server that you know you have access to. Alternatively, you can try entering a system database name like `master` and then select **Connect**. This step may not work if your system administrator has explicitly denied permissions to a guest user in the `master` database. In that scenario, you need to work with your system administrator to resolve the issue.
77+
- If the login isn't a member of the sysadmin role, enter a database name on the server that you know you have access to. Alternatively, you can try entering a system database name like `master` and then select **Connect**. This step might not work if your system administrator has explicitly denied permissions to a guest user in the `master` database. In that scenario, you need to work with your system administrator to resolve the issue.
7778

7879
### Fix the issue
7980

@@ -91,27 +92,27 @@ Use the following table to determine the appropriate action for fixing the issue
9192
| --- | --- |
9293
| No user mapping exists in the login's default database or the user has been denied access. | These database users are also referred to as orphaned users. This problem typically occurs when databases are moved between two server instances, and is one of the common causes of the 4064 error. To detect orphaned users and resolve the problem, see [Troubleshoot orphaned users (SQL Server)](../../sql-server/failover-clusters/troubleshoot-orphaned-users-sql-server.md). |
9394
| No database user exists for the login | [Create a database user](../security/authentication-access/create-a-database-user.md) and assign relevant permissions to access the database. |
94-
| Database user account denied permissions to access the database | Navigate to the user properties in the database (Expand database node > **Security** > **Users**) and check if the user is part of **db_denydatareader** role in the **Membership** page. You can also check effective permissions of a user using [sys.fn_my_permissions](../system-functions/sys-fn-my-permissions-transact-sql.md). |
95-
| The default database is in suspect mode. | A database can become suspect for several reasons. Possible causes include denial of access to a database resource by the operating system and the unavailability or corruption of one or more database files. You can check the state of the database using this query: `SELECT DATABASEPROPERTYEX (N'<dbname>', N'STATUS') AS N'Database Status';`. In SSMS, the status of suspect databases shows as (Recovery Pending). You may have to recover the database from its backup to resolve this situation. |
95+
| Database user account denied permissions to access the database | Navigate to the user properties in the database (Expand database node > **Security** > **Users**) and check if the user is part of **db_denydatareader** role in the **Membership** page. You can also check effective permissions of a user by using [sys.fn_my_permissions](../system-functions/sys-fn-my-permissions-transact-sql.md). |
96+
| The default database is in suspect mode. | A database can become suspect for several reasons. Possible causes include denial of access to a database resource by the operating system and the unavailability or corruption of one or more database files. You can check the state of the database by using this query: `SELECT DATABASEPROPERTYEX (N'<dbname>', N'STATUS') AS N'Database Status';`. In SSMS, the status of suspect databases shows as (Recovery Pending). You might have to recover the database from its backup to resolve this situation. |
9697
|Incorrect database name in connection string|When you attempt to connect to a database that doesn't exist, you might see the following error message:<br/>`Cannot open database "AdventureWorks" requested by the login. The login failed.`<br/>The database management system (DBMS) might also show the `Login failed for user CONTOSO\user1` error message. For more information, see [MSSQLSERVER_18456](mssqlserver-18456-database-engine-error.md).<br/>The SQL Server error log will have the following message:<br/>"Login failed for user 'CONTOSO\User1'. Reason: Failed to open the explicitly specified database 'AdventureWorks'."<br/>To resolve this error, make sure that the database name is the same in both the error message and the error log entry. Change the connection string if it's incorrect, or grant the user the required permissions.|
97-
| The default database no longer exists. | If you've intentionally removed the database from the server, change the default database for the login to another existing database on the server using SSMS or an [ALTER LOGIN (Transact-SQL)](../../t-sql/statements/alter-login-transact-sql.md) statement. Optionally, you may want to check if there are other logins on the server whose default database is set to this non-existing database by using this query: `SELECT name AS Login_Name FROM sys.server_principals where default_database_name = '<removed-dbname>';`. |
98+
| The default database no longer exists. | If you've intentionally removed the database from the server, change the default database for the login to another existing database on the server by using SSMS or an [ALTER LOGIN (Transact-SQL)](../../t-sql/statements/alter-login-transact-sql.md) statement. Optionally, you might want to check if there are other logins on the server whose default database is set to this non-existing database by using this query: `SELECT name AS Login_Name FROM sys.server_principals where default_database_name = '<removed-dbname>';`. |
9899
| The default database is in a single-user mode, and the only connection is being used by the administrator or someone else. | If the database is set to single-user mode for maintenance purposes, you should set it back to multi-user mode after the completion of maintenance activity, using this query: `ALTER DATABASE <dbname> SET MULTI_USER;`.<br />For more information, see [Set a database to single-user mode](../databases/set-a-database-to-single-user-mode.md).<br />**Note**: To check if a database is in single-user mode, you can use this query: `SELECT name, user_access_desc FROM sys.databases WHERE name = '<dbname>';`.<br />If you still need to restrict access to the database but want to enable connectivity for the affected logins, change their default database to a different database on the server. |
99-
| The default database has been detached. | Detaching a database removes it from the instance of SQL Server and is no longer accessible. To make it available for logins, attach the database using SSMS or an [sp_attach_db](../system-stored-procedures/sp-attach-db-transact-sql.md) stored procedure. For more information, see [Database Detach and Attach (SQL Server)](../databases/database-detach-and-attach-sql-server.md). |
100+
| The default database has been detached. | Detaching a database removes it from the instance of SQL Server and is no longer accessible. To make it available for logins, attach the database by using SSMS or an [sp_attach_db](../system-stored-procedures/sp-attach-db-transact-sql.md) stored procedure. For more information, see [Database Detach and Attach (SQL Server)](../databases/database-detach-and-attach-sql-server.md). |
100101
| The default database has been set to a RESTRICTED_USER state. | When a database is set to a RESTRICTED_USER state, only members of the db_owner fixed database role and dbcreator and sysadmin fixed server roles can connect to the database. If you no longer need to restrict access to the corresponding database, set the database to multi-user mode using this query: `ALTER DATABASE <dbname> SET MULTI_USER;`.<br />**Note**: To check if a database is in a restricted-user state, you can use this query: `SELECT name, user_access_desc FROM sys.databases WHERE name = '<dbname>';`.<br />If you still need to restrict access to the database but want to enable connectivity for the affected logins, change their default database to a different database on the server. |
101102
| The default database is offline. | A database that is in the offline state can't be modified. You can bring your database online using this query: `ALTER database <dnname> SET ONLINE;`.<br />You can check if a database is OFFLINE either using SSMS, or using this query: `SELECT DATABASEPROPERTYEX (N'<dbname>', N'STATUS') AS N'Database Status';`.<br />For more information, see [Database States](../databases/database-states.md) and [ALTER DATABASE SET Options (Transact-SQL) - SQL Server](../../t-sql/statements/alter-database-transact-sql-set-options.md).<br />If you still need to keep the database in an offline state but want to enable connectivity for the affected logins, change their default database to a different database on the server. |
102-
| The default database is set to an EMERGENCY state. | A database may have been put into an EMERGENCY state for troubleshooting by a system administrator. Only users of the sysadmin role can access databases set to an EMERGENCY state. You can bring your database online using this query: `ALTER database <dnname> SET ONLINE;`.<br />You can check if a database is in an EMERGENCY state either using SSMS, or this query: `SELECT DATABASEPROPERTYEX (N'<dbname>', N'STATUS') AS N'Database Status';`.<br />For more information, see [Database States](../databases/database-states.md) and [ALTER DATABASE SET Options (Transact-SQL)](../../t-sql/statements/alter-database-transact-sql-set-options.md).<br />If you still need to keep the database in an EMERGENCY state, but want to enable connectivity for the affected logins, change their default database to a different database on the server. |
103+
| The default database is set to an EMERGENCY state. | A database might have been put into an EMERGENCY state for troubleshooting by a system administrator. Only users of the sysadmin role can access databases set to an EMERGENCY state. You can bring your database online using this query: `ALTER database <dnname> SET ONLINE;`.<br />You can check if a database is in an EMERGENCY state either using SSMS, or this query: `SELECT DATABASEPROPERTYEX (N'<dbname>', N'STATUS') AS N'Database Status';`.<br />For more information, see [Database States](../databases/database-states.md) and [ALTER DATABASE SET Options (Transact-SQL)](../../t-sql/statements/alter-database-transact-sql-set-options.md).<br />If you still need to keep the database in an EMERGENCY state, but want to enable connectivity for the affected logins, change their default database to a different database on the server. |
103104
| The default database is part of the database mirror. | You can't connect to a mirror database on the mirror server, and this behavior is by design. To check if the database is currently in mirror role, use this query `SELECT DB_NAME(database_id) as database_name, mirroring_role_desc FROM sys.database_mirroring WHERE DB_NAME(database_id) = '<dbname>';`. For more information about database mirroring, see [Database Mirroring (SQL Server)](../../database-engine/database-mirroring/database-mirroring-sql-server.md). |
104-
| The login account may be a member of multiple groups, and the default database for one of the groups is unavailable at the time of connection. | To enumerate the groups that have a specified user using PowerShell, see [Get-ADPrincipalGroupMembership (ActiveDirectory)](/powershell/module/activedirectory/get-adprincipalgroupmembership). |
105+
| The login account might be a member of multiple groups, and the default database for one of the groups is unavailable at the time of connection. | To enumerate the groups that have a specified user by using PowerShell, see [Get-ADPrincipalGroupMembership (ActiveDirectory)](/powershell/module/activedirectory/get-adprincipalgroupmembership). |
105106

106107
## Change the default database for a given user
107108

108109
To make changes to a user's default database, you need to have ALTER ANY LOGIN permission. If the login being changed is a member of the sysadmin fixed server role or a grantee of CONTROL SERVER permission, it also requires CONTROL SERVER permission when making the following changes. Members of the sysadmin role have these permissions enabled by default. For more information, see [ALTER LOGIN (Transact-SQL)](../../t-sql/statements/alter-login-transact-sql.md).
109110

110111
# [SQL Server Management Studio](#tab/ssms)
111112

112-
### Change the default database using SSMS
113+
### Change the default database by using SSMS
113114

114-
1. Connect to your SQL Server instance using SQL Server Management Studio (SSMS).
115+
1. Connect to your SQL Server instance by using SQL Server Management Studio (SSMS).
115116

116117
1. Select **Security** > **Logins** to locate the user and change user's default database to a different one that's currently available in the **General** page of login's properties in SSMS. For more information, see [Create a Login](../security/authentication-access/create-a-login.md).
117118

@@ -130,15 +131,15 @@ To make changes to a user's default database, you need to have ALTER ANY LOGIN p
130131

131132
# [sqlcmd utility](#tab/sqlcmd)
132133

133-
### Change the default database using the sqlcmd utility
134+
### Change the default database by using the sqlcmd utility
134135

135-
You can use the following procedure to change the default database using the [sqlcmd utility](../../tools/sqlcmd/sqlcmd-utility.md):
136+
You can use the following procedure to change the default database by using the [sqlcmd utility](../../tools/sqlcmd/sqlcmd-utility.md):
136137

137138
1. On the computer running SQL Server, select **Start**, then select **Run**, enter `cmd`, and then press **Enter**.
138139

139-
1. Connect to SQL Server using the **sqlcmd** utility and one of the following options:
140+
1. Connect to SQL Server by using the **sqlcmd** utility and one of the following options:
140141

141-
- If you want to use Windows authentication to connect to the instance using the Windows user account currently logged on, enter the following command in the **Command Prompt** window, and then press **Enter**:
142+
- If you want to use Windows authentication to connect to the instance by using the Windows user account currently logged on, enter the following command in the **Command Prompt** window, and then press **Enter**:
142143

143144
`sqlcmd -E -S <InstanceName> -d master`
144145

@@ -179,10 +180,13 @@ You can use the following procedure to change the default database using the [sq
179180

180181
# [PowerShell](#tab/powershell)
181182

182-
### Change the default database using PowerShell
183+
### Change the default database by using PowerShell
183184

184185
You can use the following PowerShell script to update the default database for a specified login.
185186

187+
> [!NOTE]
188+
> The following script requires the **Microsoft.Data.SqlClient** assembly. Install it from the [NuGet package](https://www.nuget.org/packages/Microsoft.Data.SqlClient) and ensure the DLL is accessible to PowerShell. You can load it with `Add-Type -Path "path\to\Microsoft.Data.SqlClient.dll"` before running the script.
189+
186190
```powershell
187191
# Define the variables for the SQL Server instance name and login name
188192
@@ -193,19 +197,19 @@ $newDefaultDB = "YOUR_NEW_DEFAULT_DATABASE"
193197
# Connect to the SQL Server instance
194198
195199
$connectionString = "Server=$instanceName;Integrated Security=True"
196-
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
200+
$connection = New-Object Microsoft.Data.SqlClient.SqlConnection($connectionString)
197201
$connection.Open()
198202
199203
# Execute a T-SQL command to change the default database for the specified login
200204
201205
$tsql = "ALTER LOGIN $loginName WITH DEFAULT_DATABASE = $newDefaultDB"
202-
$command = New-Object System.Data.SqlClient.SqlCommand($tsql, $connection)
206+
$command = New-Object Microsoft.Data.SqlClient.SqlCommand($tsql, $connection)
203207
$command.ExecuteNonQuery()
204208
205209
# Print a message indicating the update was successful
206-
210+
207211
Write-Host "The default database for the login '$loginName' has been successfully updated to '$newDefaultDB'"
208-
212+
209213
# Close the SQL Server connection
210214
211215
$connection.Close()
@@ -215,7 +219,7 @@ $connection.Close()
215219

216220
## Error 18456 is displayed along with error 4064
217221

218-
When using applications like SSMS that get error 4064 displayed to the user, the following message is logged in the SQL Server error log. This behavior is by design. Fixing the default database for the failed login, using the procedures documented in this article, automatically addresses the 18456 error.
222+
When you use applications like SSMS that get error 4064 displayed to the user, the following message is logged in the SQL Server error log. This behavior is by design. Fixing the default database for the failed login by using the procedures documented in this article automatically addresses the 18456 error.
219223

220224
```output
221225
2023-02-06 18:17:02.56 Logon Error: 18456, Severity: 14, State: 40.

0 commit comments

Comments
 (0)