I am using SQL Server 2008 developer edition. I was trying to attach the AdventureWorks2008 database.
When I tried to attach, I received an "access is denied" error. According to the event log, it came from the O/S:
Open failed: Could not open file D:\ProjectData\AdventureWorks\AdventureWorksLT2008_Data.mdf for file number 0. OS error: 5(Access is denied.).
I thought "NTFS problem", but System (and I) have modify access to both files.
I found that I can successfully attach the database if I log in as sa, but my user account won't work.
I am a member of the local administrators group on my machine, and I am in the sysadmins role in SQL Server instance.
Any idea why I had to be logged in as sa?
Thank you for all of the comments. Some of you helped to lead me to the answer. Here's what I found:
It was an NTFS permission problem, and not a SQL problem. Further, it looks kind of bug-like (and it's repeatable).
The problem: The account that I was using had full control NTFS permissions to the mdf and ldf files. However, it had those permissions through group membership (the Local Administrators group had permissions, and my account is a member of local admins). (I verified the permissions)
If I try to do the attach, connect to SQL Server as me (where I am in the admins group), it fails with the NTFS problem.
However, if I grant the same file permissions that the local admin group has directly to my Domain Account, then I can attach with no problems.
(oh, and yes, I checked the local groups on this machine, and I verified that my domain account is indeed a member of the local admins group).
So, it looks as though the error occurs because some code (either in SQL Server or Management Studio) checks for the permissions that the user account holds, but it doesn't go so far as to check group permissions that the user account inherits.
That sounds weird to me, but I can reproduce it over and over again, so I have concluded that it is the answer.
Update: I reported this as a bug: https://connect.microsoft.com/SQLServer/feedback/details/539703/access-denied-attaching-a-database-when-permissions-are-inherited
Add permission to the folder where your
.mdf
file is.Check this name:
NT Service\MSSQLSERVER
And change the
Location
to your server name.I have solved the problem by just move the .mdf file that you want to attach to the public folder, in my case I moved it to the users/public folder. Then I attach it from there without any problem. Hope this helps.
A SQL2005 database can be attached in this way in Windows 7:
And then attached database successfully completed.
I got this error as sa. In my case, security didn't matter. I added everyone full control to the mdf and ldf files, and attach went fine.
For those who could not fix the problem with the other solutions here, the following fix worked for me:
Go to your "DATA" folder in your SQL Server installation, right click, properties, security tab, and add full control permissions for the "NETWORK SERVICE" user.
http://decoding.wordpress.com/2008/08/25/sql-server-2005-expess-how-to-fix-error-3417/
(The above link is for SQL 2005, but this fixed a SQL 2008 R2 installation for me).
Some additional info: This problem showed up for me after replacing a secondary hard drive (which the SQL installation was on). I copied all the files, and restored the original drive letter to the new hard disk. However, the security permissions were not copied over. I think next time I will use a better method of copying data.