Troubleshooting Guide: Error 9236 – “Database %.*ls is not a valid local database”
Why This Error Happens
This error occurs when SQL Server 2025 attempts to access a database file (typically an .mdf or .ldf file) that is either corrupted, mismatched with its associated log file, or was created by an incompatible version of SQL Server. It can also surface when a LocalDB instance’s metadata becomes desynchronized from the actual file structure on disk, often due to an improper shutdown or abrupt termination of the SQL Server process. Additionally, this issue is commonly triggered when a database file has been manually copied or moved without detaching it first, leaving the internal file header pointing to an invalid or non-existent local database reference.
Step-by-Step Resolution Guide
Step 1: Verify the LocalDB Instance Status
- Open Command Prompt as Administrator on your Windows Server 2025 / Windows 11 machine.
- Run
sqllocaldb infoto list all existing LocalDB instances. - Run
sqllocaldb info MSSQLLocalDB(or your specific instance name) to check its current state (Running, Stopped, or Corrupted). - If the instance shows as corrupted or unresponsive, proceed to the next step.
Step 2: Stop and Delete the Corrupted LocalDB Instance
- Stop the instance using:
sqllocaldb stop MSSQLLocalDB -i - Delete the broken instance registration with:
sqllocaldb delete MSSQLLocalDB - Navigate to
C:\Users\<YourUsername>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\and manually remove the leftover instance folder if it still exists. - Recreate a fresh instance using:
sqllocaldb create MSSQLLocalDB
Step 3: Re-attach the Database Files Correctly
- Open SQL Server Management Studio (SSMS) 2025 and connect to
(localdb)\MSSQLLocalDB. - Use
sp_detach_dbon the source server (if accessible) before moving files in the future to avoid header corruption. - Attach the
.mdffile manually via:CREATE DATABASE YourDBName ON (FILENAME = 'C:\Path\To\YourDB.mdf') FOR ATTACH; - If the
.ldflog file is missing or mismatched, useFOR ATTACH_REBUILD_LOGinstead to force SQL Server to regenerate a new log file.
Step 4: Check File Permissions and Integrity
- Right-click the database folder (commonly located in
C:\Users\<YourUsername>\Documents\or your project’sApp_Datafolder) and go to Properties > Security. - Ensure the
NT AUTHORITY\NETWORK SERVICEand your local Windows user account have Full Control permissions. - Run
CHKDSK C: /Fvia an elevated Command Prompt to rule out underlying disk corruption affecting the.mdf/.ldffiles.
Alternative or Verification Steps
- To verify the fix, run
SELECT name, state_desc FROM sys.databases;in SSMS and confirm the database state reads ONLINE rather than SUSPECT or RECOVERY_PENDING. - If the issue persists, restore the database from a recent
.bakbackup file usingRESTORE DATABASE YourDBName FROM DISK = 'C:\Backups\YourDB.bak' WITH REPLACE;as a fallback to bypass the corrupted local file entirely.