sp_attach_single_file_db 错误:失败,操作系统错误 5(访问被拒绝。)
我正在尝试使用来自 MS 的示例项目附带的数据库:
http://code.msdn.microsoft.com/ASPNET-Web-Forms-6c7197aa/sourcecode?fileId=18930&pathId=365206059
因此,在我下载文件后:我需要附加.mdf 数据库到我的 MS SQL 2008 实例。
从 Management Studio 附加数据库不起作用,并且使用此命令的事件我收到相同的错误:
sp_attach_single_file_db 'School.mdf', 'C:\School.mdf'
错误:
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.).
知道出了什么问题吗?感谢您的帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
授予要在其中创建 mdf 文件的文件夹的完全权限
到运行 SQL Server 服务的登录帐户。
通常是:NT Service\MSSQL$
你可以查一下
控制面板->管理工具->服务=> SQL服务器->属性->登录选项卡->记下账号
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
您在什么操作系统上运行?你把文件保存到C盘根目录的时候有没有提升权限的提示? SQL Server 运行在什么用户帐户下?它是否有权读取 C 盘根目录中的任何文件?
您最好将该文件与您需要的其他
.mdf
文件一起放入%ProgramFiles%\Microsoft SQL Server\MSSQL10.\MSSQL\DATA
中。知道它已经可以读取(根据需要调整上面的路径,但希望您能明白)。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).我遇到了这个问题,网上的所有解决方案都具有误导性。
解决方案是以管理员身份打开 SSMS。我认为首先尝试这个,然后再尝试所有其他解决方案。
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.
将其移至 DATA 文件夹基本上解决了权限问题。另一个解决方案是更改文件(或多个文件)的权限,因此在不同的文件夹中仍然有数据库。我刚刚遇到类似的权限错误“创建文件遇到操作系统错误 5(访问被拒绝。)”尝试在 Windows 7 上的 SQL Server 2012 Express 中附加数据库。
我所做的解决方案如下:
1) 右键单击每个 MDF、LDF 和 NDF 文件以获取菜单 -->选择属性
2)为计算机添加用户并给予他们完全控制权(而不是每个人 - 危险)
之后我可以附加数据库。
但是,即使没有移至 DATA 文件夹,也应移至子文件夹而不是 C:\ root(例如:C:\mydb)。
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).
上创建数据库 newDataBaseName
在 (FILENAME = 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\filename.mdf')、
(FILENAME = 'C:\Program Files (x86)\Microsoft SQL) 服务器\MSSQL12.SQLEXPRESS\MSSQL\DATA\filename_log.ldf')
FOR ATTACH_REBUILD_LOG;
GO
另外,正如 Damien 上面提到的,将 mdf 文件放在其他 mdf 文件所在的位置。对我来说 C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA
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.