You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: docs/relational-databases/errors-events/mssqlserver-4064-database-engine-error.md
+28-24Lines changed: 28 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,8 @@ description: "MSSQLSERVER_4064"
4
4
author: MashaMSFT
5
5
ms.author: mathoma
6
6
ms.reviewer: sureshka, randolphwest
7
-
ms.date: 01/10/2024
7
+
ms.date: 03/17/2026
8
+
ai-usage: ai-assisted
8
9
ms.service: sql
9
10
ms.subservice: supportability
10
11
ms.topic: "reference"
@@ -37,19 +38,19 @@ Permission issues can be one or more of the following:
37
38
- You assigned a default database to the login but didn't create a user mapping in the specified database.
38
39
- 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.)
39
40
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:
41
42
42
43
- The default database is in suspect mode.
43
44
- The default database no longer exists.
44
-
- The default database name is not correct.
45
+
- The default database name isn't correct.
45
46
- The default database is in single-user mode, and the only available connection is already used by someone or something else.
46
47
- The default database has been detached.
47
48
- The default database is set to a RESTRICTED_USER state.
48
49
- The default database is offline.
49
50
- The default database is set to an EMERGENCY state.
50
51
- The default database is part of a database mirror.
51
52
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.
53
54
54
55
For more information about database users in SQL Server, see [Create a Database User](../security/authentication-access/create-a-database-user.md).
55
56
@@ -59,7 +60,7 @@ You can take one of the following actions:
59
60
60
61
### Work around the issue
61
62
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:
63
64
64
65
1. Open SQL Server Management Studio (SSMS).
65
66
@@ -73,7 +74,7 @@ If you don't need to access the currently configured default database and you ju
73
74
74
75
- 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).
75
76
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.
77
78
78
79
### Fix the issue
79
80
@@ -91,27 +92,27 @@ Use the following table to determine the appropriate action for fixing the issue
91
92
| --- | --- |
92
93
| 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). |
93
94
| 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. |
96
97
|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>';`. |
98
99
| 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). |
100
101
| 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. |
101
102
| 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. |
103
104
| 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). |
105
106
106
107
## Change the default database for a given user
107
108
108
109
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).
109
110
110
111
# [SQL Server Management Studio](#tab/ssms)
111
112
112
-
### Change the default database using SSMS
113
+
### Change the default database by using SSMS
113
114
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).
115
116
116
117
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).
117
118
@@ -130,15 +131,15 @@ To make changes to a user's default database, you need to have ALTER ANY LOGIN p
130
131
131
132
# [sqlcmd utility](#tab/sqlcmd)
132
133
133
-
### Change the default database using the sqlcmd utility
134
+
### Change the default database by using the sqlcmd utility
134
135
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):
136
137
137
138
1. On the computer running SQL Server, select **Start**, then select **Run**, enter `cmd`, and then press **Enter**.
138
139
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:
140
141
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**:
142
143
143
144
`sqlcmd -E -S <InstanceName> -d master`
144
145
@@ -179,10 +180,13 @@ You can use the following procedure to change the default database using the [sq
179
180
180
181
# [PowerShell](#tab/powershell)
181
182
182
-
### Change the default database using PowerShell
183
+
### Change the default database by using PowerShell
183
184
184
185
You can use the following PowerShell script to update the default database for a specified login.
185
186
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
+
186
190
```powershell
187
191
# Define the variables for the SQL Server instance name and login name
# Print a message indicating the update was successful
206
-
210
+
207
211
Write-Host "The default database for the login '$loginName' has been successfully updated to '$newDefaultDB'"
208
-
212
+
209
213
# Close the SQL Server connection
210
214
211
215
$connection.Close()
@@ -215,7 +219,7 @@ $connection.Close()
215
219
216
220
## Error 18456 is displayed along with error 4064
217
221
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.
0 commit comments