简体   繁体   English

无法连接到.mdf数据库

[英]Cannot connect to .mdf database

I've tried everything but I can not access/change/modify my database. 我已经尝试了一切,但我无法访问/更改/修改我的数据库。 I created it in visual studio with .mdf. 我用.mdf在visual studio中创建了它。 I am new at using SQL database so I will be glad if you can help me. 我是使用SQL数据库的新手,所以如果你能帮助我,我会很高兴的。 As I said, I created that database, in visual studio but I can not create a connection with that database. 正如我所说,我在visual studio中创建了该数据库,但我无法与该数据库建立连接。

In web config : 在网络配置中:

<connectionStrings>
   <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|Database.mdf;Integrated Security=True;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

In cs I used that sql connection string: 在cs中我使用了sql连接字符串:

SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFilename=|DataDirectory|Database.mdf;Integrated Security=True;");

And I have this error: 我有这个错误:

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


I've made some changes: 我做了一些改变:

In web config : 在网络配置中:

<connectionStrings>
 <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=|DataDirectory|Database.mdf;Integrated Security=True;"
  providerName="System.Data.SqlClient" />

and in cs: 并在cs中:

 SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=|DataDirectory|Database.mdf;Integrated Security=True;");

Now it gives that error: 现在它给出了这个错误:

*Server Error in '/WebSite1' Application. Cannot open database |DataDirectory|Database.mdf" requested by the login. The login failed. Login failed for user 'mcan-PC\mcan'. *

As the error clearly states, that database is already connected to SQL Server. 由于错误明确指出,该数据库已连接到SQL Server。
You can't have two databases with the same name on the same server. 您不能在同一服务器上拥有两个具有相同名称的数据库。

The problem could be that your mdf file doesn't have permissions set for Authenticated Users. 问题可能是您的mdf文件没有为Authenticated Users设置权限。 Navigate to your .mdf file, right click and go to Properties then the Security tab. 导航到.mdf文件,右键单击并转到“属性”,然后选择“安全”选项卡。 Check if the Authenticated Users appears in 'Group or user names'. 检查Authenticated Users是否出现在“组或用户名”中。 If it doesn't then you'll have to click on Edit then Add and type in Authenticated Users. 如果没有,则您必须单击“编辑”,然后单击“添加”并键入“经过身份验证的用户”。 Then you'll hit Check names and OK. 然后你会点击检查名称然后确定。 After that enable full control for Authenticated Users. 之后,启用对Authenticated Users的完全控制。 You'll have to repeat this for the .ldf file too. 您还必须为.ldf文件重复此操作。

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

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