Incorrect connection string? Error: An attempt to attach an auto-named database failed
Incorrect connection string? Error: An attempt to attach an auto-named database failed
解决方案:将目录添加到项目属性部分的 app.config 文件和 Settings.setting 文件中的连接字符串。我的工作连接字符串最终是 <Value Profile="(Default)">Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\hi\prgrm\ProgramName\Database1.mdf;Integrated Security=True</Value>
一旦我 运行 我的程序出现以下错误:
An attempt to attach an auto-named database for file F:\Graded unit
2\SimplyRugby\LollipopUI\bin\Debug\Database1.mdf failed. A
database with the same name exists, or specified file cannot be
opened, or it is located on UNC share.
导致错误的方法:
public bool CheckUsername(string username)
{
var usernameResult = (from person in dbContext.Persons
where (person.Username == username)
select person.Username).FirstOrDefault();
//stores username if a username is found
return !(string.IsNullOrEmpty(usernameResult));
// if no correct user found from query return false else true
}
经过一番研究,显然是连接字符串错误。我对网上的一些建议进行了一些尝试,但我不太确定哪里不正确以及如何解决它,所以在过去的五个多小时里我一直在兜圈子。
我的 app.config
文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="LollipopUI.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
感谢您的宝贵时间。
尝试使用户实例属性像
一样真实
User Instance=True
在您的连接字符串中。
您还可以参考此 related thread,其中说明您的连接字符串应如下所示:
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
晚回复总比没有回复好:
问题是我的 connectionString 没有映射到数据库位置。
编辑:我不确定我使用的是很久以前的路径,但如果您遇到此问题,请尝试使用 .MDF 文件的完整硬编码路径。确认它有效后,您就可以开始摆弄它了。
我遇到过这种情况,我是这样解决的。
转到 DataBase
的设置并将选项 **Copy to output Directory**
设置为 **Copy if Newer**
问题会解决
解决方案:将目录添加到项目属性部分的 app.config 文件和 Settings.setting 文件中的连接字符串。我的工作连接字符串最终是 <Value Profile="(Default)">Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\hi\prgrm\ProgramName\Database1.mdf;Integrated Security=True</Value>
一旦我 运行 我的程序出现以下错误:
An attempt to attach an auto-named database for file F:\Graded unit 2\SimplyRugby\LollipopUI\bin\Debug\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
导致错误的方法:
public bool CheckUsername(string username)
{
var usernameResult = (from person in dbContext.Persons
where (person.Username == username)
select person.Username).FirstOrDefault();
//stores username if a username is found
return !(string.IsNullOrEmpty(usernameResult));
// if no correct user found from query return false else true
}
经过一番研究,显然是连接字符串错误。我对网上的一些建议进行了一些尝试,但我不太确定哪里不正确以及如何解决它,所以在过去的五个多小时里我一直在兜圈子。
我的 app.config
文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="LollipopUI.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
感谢您的宝贵时间。
尝试使用户实例属性像
一样真实User Instance=True
在您的连接字符串中。
您还可以参考此 related thread,其中说明您的连接字符串应如下所示:
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
晚回复总比没有回复好:
问题是我的 connectionString 没有映射到数据库位置。
编辑:我不确定我使用的是很久以前的路径,但如果您遇到此问题,请尝试使用 .MDF 文件的完整硬编码路径。确认它有效后,您就可以开始摆弄它了。
我遇到过这种情况,我是这样解决的。
转到 DataBase
的设置并将选项 **Copy to output Directory**
设置为 **Copy if Newer**
问题会解决