MSSQL 错误“底层提供程序在打开时失败" [英] MSSQL Error 'The underlying provider failed on Open'
问题描述
我使用 .mdf
连接到 database
和 entityClient
.现在我想更改连接字符串,以便没有 .mdf
文件.
I was using an .mdf
for connecting to a database
and entityClient
. Now I want to change the connection string so that there will be no .mdf
file.
下面的connectionString
是否正确?
<connectionStrings>
<!--<add name="conString" connectionString="metadata=res://*/conString.csdl|res://*/conString.ssdl|res://*/conString.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.SQL2008;AttachDbFilename=|DataDirectory|NData.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />-->
<add name="conString" connectionString="metadata=res://*/conString.csdl|res://*/conString.ssdl|res://*/conString.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.SQL2008;Initial Catalog=NData;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
因为我总是收到错误:
底层提供者在打开时失败
The underlying provider failed on Open
推荐答案
我遇到了这个错误并找到了几个解决方案:
I had this error and found a few solutions:
查看您的连接字符串,它看起来有效.我找到了这篇博文,问题在这里是他们使用了集成安全性.如果您在 IIS 上运行,则您的 IIS 用户需要访问数据库.
Looking at your connection string, it looks valid. I found this blog post, the problem here is that they were using Integrated Security. If you are running on IIS, your IIS user needs access to the database.
如果您使用的是实体框架 with Transactions,Entity Framework 自动打开和关闭与每个数据库调用的连接.因此,在使用事务时,您试图将事务分散到多个连接上.这提升到 MSDTC.
If you are using Entity Framework with Transactions, Entity Framework automatically opens and closes a connection with each database call. So when using transactions, you are attempting to spread a transaction out over multiple connections. This elevates to MSDTC.
将我的代码更改为以下内容修复了它:
Changing my code to the following fixed it:
using (DatabaseEntities context = new DatabaseEntities())
{
context.Connection.Open();
// the rest
}
这篇关于MSSQL 错误“底层提供程序在打开时失败"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!