I am starting to learn SQL and I have a book that provides a database to work on. These files below are in the directory but the problem is that when I run the query, it gives me this error:
Msg 5120, Level 16, State 101, Line 1 Unable to open the physical file "C:\Murach\SQL Server 2008\Databases\AP.mdf". Operating system error 5: "5(Access is denied.)".
CREATE DATABASE AP
ON PRIMARY (FILENAME = 'C:\Murach\SQL Server 2008\Databases\AP.mdf')
LOG ON (FILENAME = 'C:\Murach\SQL Server 2008\Databases\AP_log.ldf')
FOR ATTACH
GO
In the book the author says it should work, but it is not working in my case. I searched but I do not know exactly what the problem is, so I posted this question.
SQL Server database engine service account must have permissions to read/write in the new folder.
Check out this
An old post, but here is a step by step that worked for SQL Server 2014 running under windows 7:
Voilá !
I think setting the logon account may have been an option in the installation, but if so it was not the default, and was easy to miss if you were not already aware of this issue.
This is Windows related issue where SQL Server does not have the appropriate permission to the folder that contains .bak file and hence this error.
The easiest work around is to copy your .bak file to default SQL backup location which has all the necessary permissions. You do not need to fiddle with anything else. In SQL SERVER 2012, this location is
To get around the access denied issue, I started SSMS as administrator and that allowed me to attach a database from my local drive. The database was created in another SQL and windows instance.
Yes,It's right.The first you should find out your service account of sqlserver,you can see it in Task Manager when you press ctrl+alt+delete at the same time;Then,you must give the read/write privilege of "C:\Murach\SQL Server 2008\Databases" to the service account.
The problem is due to lack of permissions for SQL Server to access the mdf & ldf files. All these procedures will work :
i had this problem. just run sql server as administrator
I solve this problem by adding
Full control
permission for both.mdf
and.ldf
files forUsers
group.The actual server permissions will not matter at this point; all looks ok. SQL Server itself needs folder permissions.
depending on your version, you can add SERVERNAME$MSSQLSERVER permissions to touch your folder. Othewise, it has to be in the default BACKUP directory (either where you installed it or default to c:\programfiles(x)\MSSQL\BACKUP.
For me it was solved in the following way with SQL Server Management studio -Log in as admin (I logged in as windows authentication) -Attach the mdf file (right click Database | attach | Add ) -Log out as admin -Log in as normal user
Even if you do the following steps you COULD get the same error message.
I still GOT the permission error, but then I noticed that in the Attach screen, the bottom section STILL showed the LOG file, and the error message remained the same.
Hope this helps someone who did the same thing.
Very Simple Solution.
I used Entity framework in my application and had this problem,I seted any permission in folders and windows services and not work, after that I start my application as administrator (right click in exe file and select "run as admin") and that works fine.
If you get this error on an
.MDF
file in theAPP_DATA
folder (or where ever you put it) for a Visual Studio project, the way I did it was to simply copy permissions from the existingDATA
folder here (I'm using SQL Express 2014 to support an older app):(note: your actual install path my vary - especially if your instance name is different)
Double click on the
DATA
folder first as administrator to make sure you have access, then open the properties on the folder and mimic the same for theAPP_DATA
folder. In my case the missing user wasMSSQL$SQLEXPRESS2014
(because I named the instanceSQLEXPRESS2014
- yours may be different). That also happens to be the SQL Server service username.