We can create (attach) a database from an .mdf file using this SQL query:
CREATE DATABASE YourDatabaseName
ON (FILENAME = 'D:\p\App_Data\aspnet-test-20260324025607.mdf')
FOR ATTACH_REBUILD_LOG;
Alternative (using SSMS)
- Open SSMS
- Right click Databases
- Click Attach
-
Select the
.mdffile - Click OK
Error: Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "D:\p\App_Data\aspnet-test-20260324025607.mdf". Operating system error 5: "5(Access is denied.)".
Msg 1802, Level 16, State 7, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Solution:
Fix (100% working solution)
Step 1: Give folder permissions
Go to:
D:\p\App_Data
Do this:
- Right click → Properties
- Go to Security tab
- Click Edit
- Click Add
Add this user:
Everyone
- Select Everyone
-
Enable:
- Full Control (Check)
- Click Apply → OK