Troubleshooting Guide: SQL Server Error 9654 – Database Does Not Exist
Why This Error Happens
Error 9654 typically occurs when the database referenced in a connection string or query has been detached, dropped, renamed, or never successfully attached to the SQL Server instance. This can also happen if the application is pointing to the wrong SQL Server instance (e.g., a named instance vs. default instance) or if the database exists but under a different name due to a case-sensitivity mismatch or restoration error. Additionally, corrupted system catalog metadata in the master database can cause SQL Server to report a valid database as “non-existent.”
Step-by-Step Resolution Guide
Step 1: Verify the Database Actually Exists on the Instance
- Open SQL Server Management Studio (SSMS) and connect to the instance.
- Run the following query to list all databases currently attached:
SELECT name, database_id, state_desc FROM sys.databases;
- Confirm the exact spelling, case, and underscores match what your application is requesting.
- If the database is missing entirely, check your backup directory (commonly
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Backup\) for a recent.bakfile to restore.
Step 2: Confirm the Application Is Targeting the Correct Instance
- Check the connection string in your application’s config file (e.g.,
web.config,appsettings.json) for theServer=orData Source=parameter. - Ensure it specifies the correct instance name, such as
SERVERNAME\INSTANCENAME, rather than defaulting tolocalhostor(local). - Open SQL Server Configuration Manager and verify the instance name under SQL Server Services matches what the application expects.
Step 3: Re-Attach or Restore the Missing Database
- If the
.mdfand.ldffiles still exist on disk (commonly underC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\), attach them manually:
CREATE DATABASE YourDatabaseName
ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\YourDatabaseName.mdf')
FOR ATTACH;
- If files are missing, restore from the latest backup using:
RESTORE DATABASE YourDatabaseName
FROM DISK = 'C:\Backups\YourDatabaseName.bak'
WITH REPLACE;
Step 4: Check for Permissions or Orphaned User Mapping Issues
- Run
EXEC sp_helpdb YourDatabaseName;to confirm the database is online and accessible. - Verify the SQL login being used by the application has proper mapping under Security > Logins > User Mapping in SSMS.
- If needed, fix orphaned users with:
ALTER USER YourUsername WITH LOGIN = YourLoginName;
Alternative or Verification Steps
- After applying the fix, restart the application and attempt the database connection again — confirm no Error 9654 appears in the SQL Server Error Log (
Management > SQL Server Logsin SSMS). - As a quick alternative check, run
SELECT DB_ID('YourDatabaseName');— if it returnsNULL, the database is still not recognized and Steps 1–3 should be repeated with closer attention to name spelling and instance targeting.