SQL sp_attach_single_file_db 错误:因操作系统错误 5 失败(访问被拒绝。)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6703368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sp_attach_single_file_db Error: failed with the operating system error 5(Access is denied.)
提问by GibboK
I'm trying to use this DataBase that come with this sample project with from MS:
我正在尝试使用来自 MS 的此示例项目附带的此数据库:
http://code.msdn.microsoft.com/ASPNET-Web-Forms-6c7197aa/sourcecode?fileId=18930&pathId=365206059
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.
所以在我下载文件后:我需要将 .mdf 数据库附加到我的 MS SQL 2008 实例。
From Management Studio Attaching DataBase does not work and event using this command i receive the same error:
从 Management Studio 附加数据库不起作用,使用此命令的事件我收到相同的错误:
sp_attach_single_file_db 'School.mdf', 'C:\School.mdf'
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!
知道出了什么问题吗?谢谢你的帮助!
回答by Sql Newbie
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$
向运行 SQL Server 服务的登录帐户授予对要在其中创建 mdf 文件的文件夹的完全权限。通常:NT Service\MSSQL$
You can look that up Control Panel-> Administrative Tools-> Services=> SQL Server -> Prperties-> Logon Tab-> Note the account
您可以查看控制面板-> 管理工具-> 服务=> SQL Server -> 属性-> 登录选项卡-> 记下帐户
回答by Damien_The_Unbeliever
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 anyfiles in the root of the C drive?
你在什么操作系统上运行?将文件保存到 C 盘根目录时是否收到提升提示?SQL Server 在哪个用户帐户下运行,它是否有权读取C 盘根目录中的任何文件?
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).
您可能会更好地将文件放入%ProgramFiles%\Microsoft SQL Server\MSSQL10.<instance name>\MSSQL\DATA
,与.mdf
您知道它已经可以读取的其他文件一起(根据需要调整上面的路径,但您希望得到这个想法)。
回答by GibboK
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.
解决方案是以管理员身份打开 SSMS。在我看来,首先尝试这个,然后再尝试所有其他解决方案。
回答by Jon Reedholm
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.
将其移动到 DATA 文件夹基本上解决了权限问题。另一种解决方案是更改文件(或多个文件)的权限,因此在不同的文件夹中仍然有 DB。我刚刚遇到类似的权限错误“创建文件遇到操作系统错误 5(访问被拒绝。)”试图在 Windows 7 上的 SQL Server 2012 express 中附加数据库。
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)
我所做的解决方案如下:1) 对每个 MDF、LDF 和 NDF 文件进行丰富的单击以获取菜单 --> 选择属性 2) 为计算机添加用户并赋予他们完全控制权(而不是每个人 - 危险)
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).
但是,即使没有移动到 DATA 文件夹,也应该移动到子文件夹而不是 C:\root(例如:C:\mydb)。
回答by Jay
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.mdf'),
(FILENAME = 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\filename_log.ldf')
(FILENAME = 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\filename_log.ldf')
FOR ATTACH_REBUILD_LOG;
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
同样正如上面提到的 Damien 将 mdf 文件放在其他 mdf 文件所在的位置。对我来说 C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA
回答by Oky
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.
就我而言,我发现目录查找需要具有读取/列表权限的计算机/用户组。虽然服务和运行(在我的情况下)还原的实际用户具有完全权限,但该权限很重要。安全明智我讨厌它。