Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
While attaching the mdf & ldf file in sql it is showing error

Unable to open the physical file "D:\Database1.mdf". Operating system error 5: "5(Access is denied.)". (Microsoft SQL Server, Error: 5120)
Posted
Updated 5-Feb-19 0:07am
v2
Comments
[no name] 6-Mar-13 10:38am    
ASP.NET? Or what? Details....
abhinavvijay 6-Mar-13 10:43am    
SQL server
[no name] 6-Mar-13 10:49am    
You have "D:\Database1.mdf" in SQL Server and get access denied? Is that really your question? Do you really have a D: drive somewhere inside your SQL Server?

Open Computer Management Console.
Expand "Services and Applications" in treeview
Select "Services"
Tab "Standard" view
Scroll down to "SQL Server ("VA" ... (in caps ... sp?)" in list, right
(See Log On As) ... could be "Local System" OR "Network Service" OR "Local Service" OR ".\(username)"
Right click "SQL Server ("VA" etc) select "Properties"
Tab to "LogOn"
Change (I mean, "experiment") either "Local System Account" OR "This Account" until error goes away
 
Share this answer
 
try this...
GO to the folder r where you have stored mdf file. select file-> Right click ->click on . give full permissions to file for logged in user Security.
 
Share this answer
 
Comments
Sharath.Mitte 23-May-14 15:52pm    
thanks amol_B, it works for me:)
satish kankerwal 19-Aug-15 8:59am    
its work
S.N.Ramkumar 18-Dec-19 9:50am    
Super Solution
Root directories are often forbidden territory for write access - and SQL server needs that in order to attach the files.
Move them to a sub folder and give full access permissions to all users for all files in the folder.
 
Share this answer
 
Comments
phil.o 7-Mar-13 5:00am    
My 5, but I would rather give full access permission only to the account running the SQL Server service.
OriginalGriff 7-Mar-13 5:08am    
Under normal circumstances, so would I - but that's a lot harder to explain how to do, particularly to what sounds like a beginner. Off the top of my head, I can't remember which user SQL server generally uses! :laugh:
phil.o 7-Mar-13 5:16am    
I think (although I'm not absolutely sure about that) that, by default, SQL Server service runs under NTAUTHORITY\Local Service account.
I agree with you it's a bit tedious to describe the stages to find which account and verify it has the correct user permissions on the server/file system.
1- Run SQL Server Management as Administrator
2- Login SQL Server with Windows Authenticate
3- Attach your DB Files
 
Share this answer
 
v2
Comments
Member 11950463 13-Feb-16 6:21am    
it actually works..thnks a lot
Step 1->Right click the SQL Server management Studio Icon
Step 2->Run as administrator
Step 3->Try attaching the database
Step-4 the database should be attached.

Thanks
Ashish
 
Share this answer
 
Comments
kefsan 7-Apr-14 11:54am    
Thanks a lot, I hope you get all your dreams.
Member 14596930 5-Nov-19 11:49am    
It Worked for me thank you.
In windows 8 the SQL Server role to add is "NT Service\MSSQLSERVER"
 
Share this answer
 
Change the permission levels to FullControl for that *.mdf file, which ever you trying to attach the database for loggedIn user account.
 
Share this answer
 
go to the file right click- property - security- full control -ok
same on log file
 
Share this answer
 
C#
Error :Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "c:\sqlserver\data\msdbtest_data.mdf". 
Operating system error 5: "5(Access is denied.)".


Log off your Computer.
And Run your SQL Management studio with Administrator
and try to Attach or Restore it.

It should work!!
 
Share this answer
 
Add the Administrators Group to the file security permissions with full control for the Data file ( S: ) and the Log File ( T: )
 
Share this answer
 
I was struggling with same issue for few days.

Solution :- Give FULL Control to the SQL Service account on the folder where .mdf , .ndf and log files staying.

Note:- If SQL Service account can not be added to network path folder in that case give Full Control to Everyone on the folder where your .mdf , .ndf and log files staying.

Warning:- Not a good practice to give Full Control on data file to everyone
 
Share this answer
 
Comments
Richard Deeming 5-Feb-19 12:33pm    
As already mentioned in solution 1.

And solution 3.

And solution 7.

And solution 8.

And solution 11.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900