简体   繁体   English

连接到SQL Server Express数据库时遇到问题

[英]Trouble connecting to SQL Server Express database

In my web application installed on IIS 7, I need to connect to a SQL Server Express database file (used for testing purposes only.) I use the following connection string in a web.config file: 在IIS 7上安装的Web应用程序中,我需要连接到SQL Server Express数据库文件(仅用于测试目的。)我在web.config文件中使用以下连接字符串:

<configuration>
  <connectionStrings>
    <add name="MyDbConnection" connectionString="data source=.\SQLEXPRESS;Integrated Security=true;AttachDBFilename='C:\Users\UserName\CSharp\WebApp004_TestDB\App_Data\Database1.mdf';User Instance=true;Connection Timeout=15" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

I also set up NTFS permissions for the C:\\Users\\UserName\\CSharp\\WebApp004_TestDB\\App_Data folder as such: 我还为C:\\Users\\UserName\\CSharp\\WebApp004_TestDB\\App_Data文件夹设置了NTFS权限,如下所示:

  • IIS_IUSRS = FullControl IIS_IUSRS = FullControl
  • IUSR = FullControl IUSR =完全控制
  • NT SERVICE\\MSSQL$SQLEXPRESS = FullControl NT SERVICE \\ MSSQL $ SQLEXPRESS = FullControl
  • IIS AppPool\\[MyAppPoolName] = FullControl IIS AppPool \\ [MyAppPoolName] = FullControl

That still didn't help. 那仍然没有帮助。 When I try to connect to that database via my C# code I get an exception: 当我尝试通过C#代码连接到该数据库时,出现异常:

An attempt to attach an auto-named database for file C:\\Users\\UserName\\CSharp\\WebApp004_TestDB\\App_Data\\Database1.mdf failed. 尝试为文件C:\\ Users \\ UserName \\ CSharp \\ WebApp004_TestDB \\ App_Data \\ Database1.mdf附加自动命名的数据库失败。 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. 存在具有相同名称的数据库,或者无法打开指定的文件,或者该数据库位于UNC共享上。

Note that the same code works on another machine with the actual SQL Server installed (with a different connection string where I used DB user name and password.) 请注意,相同的代码可在安装了实际SQL Server的另一台机器上工作(在使用DB用户名和密码的地方使用不同的连接字符串)。

Any idea what am I missing here? 知道我在这里想念什么吗?

看看这个连接字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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