简体   繁体   中英

sp_attach_single_file_db Error: failed with the operating system error 5(Access is denied.)

I'm trying to use this DataBase that come with this sample project with from MS:

http://code.msdn.microsoft.com/ASPNET-Web-Forms-6c7197aa/sourcecode?fileId=18930&pathId=365206059

So after I downloaded the files: I need to attach the.mdf DataBase to my Instance of MS SQL 2008.

From Management Studio Attaching DataBase does not work and event using this command i receive the same error:

sp_attach_single_file_db 'School.mdf', 'C:\School.mdf'


ERROR:

Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "C:\School.mdf" failed with the operating system error 5(Access is denied.).

Any idea what is wrong? Thanks for your help!

Give full permission on folder in which you want to create the mdf file to the logon account with which the SQL Server service is running. Usually: NT Service\MSSQL$

You can look that up Control Panel-> Administrative Tools-> Services=> SQL Server -> Prperties-> Logon Tab-> Note the account

What operating system are you running on? Did you get an elevation prompt when you saved the file to the root of the C drive? What user account is SQL Server running under, and does it have permissions to read any files in the root of the C drive?

You might do better placing the file into %ProgramFiles%\Microsoft SQL Server\MSSQL10.<instance name>\MSSQL\DATA , alongside the other .mdf files that you know it can already read (adjust path above as necessary, but you hopefully get the idea).

I had this issue and all the solutions online was misleading.

The solution was to open SSMS as Administrator. In my opinion first try this and later try all other solutions.

Moving it to DATA folder basically resolved permission issue. Another solution is to change the permissions of the file (or files) and thus still have DB in different folder. I just ran into similar permission error "CREATE FILE encountered operating system error 5(Access is denied.)" trying to attach DB in SQL Server 2012 express on Windows 7.

Solution I did was following: 1) Rich-click on each MDF, LDF, and NDF file to get menu --> select properties 2) Add Users for computer and give them full control (instead of Everyone - to dangerous)

After that I could attach the DB.

However, even if not moved to DATA folder, should move into subfolder instead of C:\ root (example: C:\mydb).

create DATABASE newDataBaseName

on (FILENAME = 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\filename.mdf'),

(FILENAME = 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\filename_log.ldf')

FOR ATTACH_REBUILD_LOG;

GO

Also as Damien mentioned above put the mdf files in the where your other mdf files are located. For me C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA

In my case I found the Directory lookup needed the computer/users group in read/list permissions. While the service and the actual user running the (in my case) restore had full permissions that permission was important. Security wise I hate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM