“尝试为文件 *FileName* 附加自动命名数据库失败。”从 ASP.NET Dev Server 迁移到 IIS 后出现错误
我的 ASP.NET 项目在 App_Data 文件夹中有一个 MDF 数据库。
连接字符串是:
数据 Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;集成 安全性=True;用户实例=True
工作正常,直到我将项目参数更改为使用“本地 IIS Web 服务器”而不是“Visual Studio 开发服务器”。
该项目现在产生以下 SqlException
:
尝试为文件附加自动命名数据库 C:\Users\Admin\documents\Visual Studio 2010\项目\SL\SL\App_Data\MainDatabase.mdf 失败的。存在同名数据库,或指定文件不存在 已打开,或者位于 UNC 共享上。
如何修改连接字符串以使其与 IIS 一起使用?
My ASP.NET project has an MDF database in App_Data folder.
The connection string is:
Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;Integrated
Security=True;User Instance=True
It worked fine until I changed the project parameters to use "Local IIS Web server" instead of "Visual Studio Development Server".
The project now produces the following SqlException
:
An attempt to attach an auto-named database for file
C:\Users\Admin\documents\visual studio
2010\Projects\SL\SL\App_Data\MainDatabase.mdf
failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share.
How to modify the connection string to make it work with IIS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会发现这是文件权限(错误确实指出:“或无法打开指定的文件”)。默认情况下,IIS 作为受限用户运行,并且不可能访问管理员主目录。
您的三个选项是:
有权访问该文件夹
IIS 可以访问(然后检查文件的权限)
其中,2 号是最理想的。另外两个会在一定程度上降低安全性 - 第一个,让 IIS 自由支配大量文件系统(以及潜在的系统资源);而第三个则有可能向 IIS 打开用户的主目录。
You'll probably find this is file permissions (the error does state: "or specified file cannot be opened"). IIS runs as a limited-user by default, and it's unlikely to have access to the Administrators home directory.
Your three options are:
does have access to that folder
IIS can access (and then check the permissions of the file)
Of these, number 2 is the most desirable. The other two reduce security somewhat - the first, by giving IIS free reign over a lot of the file system (and potentially system resources as well); whilst the third has the potential to open up a user's home directory to IIS.
由于您使用的是集成安全性,因此连接的用户必须映射到 SQL Server 登录名。当您更改为 IIS 时,您切换了用户,并且它可能不会映射到数据库中的登录名。您可以通过指定数据库中有效的用户名和密码来测试这一点。
例如:
数据源=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;用户ID=admin;密码=密码;用户实例=True
Since you are using Integrated Security, the user connecting has to map to a SQL Server login. When you changed to IIS, you switched the user and it may not map to a login in the DB. You could test this by specifying a username and password that are valid in your database.
For example:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;User Id=admin;Password=password;User Instance=True
确保您的 IIS 进程正在运行的用户帐户对该文件具有正确的权限。
Make sure the user account your IIS process is running as has correct permissions to that file.