I have SQL Server Express installed on my local machine. When I try to attach a database file, it's giving me the following error

Unable to open physical file "C:\Adventures2012_data.mdf. Operating system error 5: 5 (Access is denied) (Microsoft sql server error 5120)

Please let me know how to fix this??

migrated from stackoverflow.com Sep 3 '13 at 2:46

This question came from our site for professional and enthusiast programmers.

  • 1
    Access is denied is quite a self explanatory error. – Ash Burlaczenko Sep 2 '13 at 18:28
  • move the file to some other folder having a full permission or execute permission for the SQL service account and then try to attach file. – Renju Chandran chingath Sep 3 '13 at 5:20

The minimal permissions needed to attach a database file is 'full control'. If you want to attach the database using SQL Server Management Studio, the account running SSMS must have full control on both the database MDF and LDF files

To grant privileges to files:

  1. Right click the file in Windows Explorer
  2. Open the Security tab
  3. Click Edit
  4. Click Add and add the SSMS account
  5. Grant Full control
  6. Go to SSMS and try to attach the file again

You can copy the file to another folder, to avoid C root, you might not have enough permissions there

  • Worked for me tooooo. One addition from my end: I have multiple databases inside a folder so I Right clicked on the Folder instead of file and follow the above process. All databases inside that folder are working now. – MaxPayne Oct 27 '15 at 7:03

1) right click the .mdf file 2) Propertiest 3) Security 4) edit 5) add 6) Advanced 7) find now 8) everyone 9) okay 10) okay 11) Full controll 12) okey 13) okey 14) right click sql server management studio 15) run as administrator 16) Then Attach.

Good luck

I had the same problem with the AdventureWorks database. I changed the directory from C:\ to E:\, and then right-clicked on the AdventureWorks database, selected Security, and granted the Full Control permission to your local users. Finally, you can run SQL Management Studio as Administrator, and attach your database.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.