简体   繁体   English

MSSQL 错误“底层提供程序在打开时失败”

[英]MSSQL Error 'The underlying provider failed on Open'

I was using an .mdf for connecting to a database and entityClient .我使用.mdf连接到databaseentityClient Now I want to change the connection string so that there will be no .mdf file.现在我想更改连接字符串,以便没有.mdf文件。

Is the following connectionString correct?以下connectionString是否正确?

<connectionStrings>
   <!--<add name="conString" connectionString="metadata=res://*/conString.csdl|res://*/conString.ssdl|res://*/conString.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQL2008;AttachDbFilename=|DataDirectory|\NData.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True&quot;" 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=&quot;Data Source=.\SQL2008;Initial Catalog=NData;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

Because I always get the error:因为我总是得到错误:

The underlying provider failed on Open基础提供程序在打开时失败

I had this error and found a few solutions:我遇到了这个错误并找到了一些解决方案:

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.如果您在 IIS 上运行,您的 IIS 用户需要访问数据库。

If you are using Entity Framework with Transactions , Entity Framework automatically opens and closes a connection with each database call.如果您将Entity Framework 与 Transactions一起使用,Entity Framework 会自动打开和关闭与每个数据库调用的连接。 So when using transactions, you are attempting to spread a transaction out over multiple connections.因此,在使用事务时,您试图将事务分散到多个连接上。 This elevates to MSDTC .这提升到MSDTC

( See this reference for more information. ) 有关更多信息,请参阅此参考。

Changing my code to the following fixed it:将我的代码更改为以下修复它:

using (DatabaseEntities context = new DatabaseEntities())
{
    context.Connection.Open();
    // the rest
}

context.Connection.Open() didn't help solving my problem so I tried enabling "Allow Remote Clients" in DTC config, no more error. context.Connection.Open()没有帮助解决我的问题,所以我尝试在 DTC 配置中启用“允许远程客户端”,没有更多错误。

In windows 7 you can open the DTC config by running dcomcnfg, Component Services -> Computers -> My Computer -> Distributed Transaction Coordinator -> Right click to Local DTC -> Security.在 Windows 7 中,您可以通过运行 dcomcnfg、组件服务 -> 计算机 -> 我的电脑 -> 分布式事务协调器 -> 右键单击​​本地 DTC -> 安全来打开 DTC 配置。

You should see innerException to see what the inner cause of throwing of error is.您应该看到innerException以了解引发错误的内部原因是什么。

In my case, the original error was:就我而言,最初的错误是:

Unable to open the physical file "D:\Projects2\xCU\xCU\App_Data\xCUData_log.ldf".无法打开物理文件“D:\Projects2\xCU\xCU\App_Data\xCUData_log.ldf”。 Operating system error 5: "5(Access is denied.)".操作系统错误 5:“5(拒绝访问。)”。 An attempt to attach an auto-named database for file D:\Projects2\xCU\xCU\App_Data\xCUData.mdf failed.尝试为文件 D:\Projects2\xCU\xCU\App_Data\xCUData.mdf 附加自动命名数据库失败。 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。

which solved by giving full permission to current user for accessing related mdf and ldf files using files' properties.通过授予当前用户使用文件属性访问相关mdfldf文件的完全权限来解决此问题。

I found the problem was that I had the server path within the connection string in one of these variants:我发现问题是我在这些变体之一中的连接字符串中有服务器路径:

SERVER\SQLEXPRESS
SERVER

When really I should have:当我真的应该拥有:

.\SQLEXPRESS

For some reason I got the error whenever it had difficulty locating the instance of SQL.出于某种原因,每当难以定位 SQL 实例时,我都会收到错误消息。

This is common issue only.这只是常见问题。 Even I have faced this issue.甚至我也遇到过这个问题。 On the development machine, configured with Windows authentication, it is worked perfectly:在配置了 Windows 身份验证的开发机器上,它可以完美运行:

<add name="ShoppingCartAdminEntities" connectionString="metadata=res://*/ShoppingCartAPIModel.csdl|res://*/ShoppingCartAPIModel.ssdl|res://*/ShoppingCartAPIModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQlExpress;initial catalog=ShoppingCartAdmin;Integrated Security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Once hosted in IIS with the same configuration, I got this error:一旦托管在具有相同配置的 IIS 中,我收到此错误:

The underlying provider failed on Open基础提供程序在打开时失败

It was solved changing connectionString in the configuration file:在配置文件中更改connectionString已解决:

<add name="MyEntities" connectionString="metadata=res://*/ShoppingCartAPIModel.csdl|res://*/ShoppingCartAPIModel.ssdl|res://*/ShoppingCartAPIModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MACHINE_Name\SQlExpress;initial catalog=ShoppingCartAdmin;persist security info=True;user id=sa;password=notmyrealpassword;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Other common mistakes could be:其他常见错误可能是:

  1. Database service could be stopped数据库服务可能会停止
  2. Data Source attributes pointing to a local database with Windows authentication and hosted in IIS指向具有 Windows 身份验证并托管在 IIS 中的本地数据库的数据源属性
  3. Username and password could be wrong.用户名和密码可能有误。

When you receive this exception, make sure to expand the detail and look at the inner exception details as it will provide details on why the login failed.当您收到此异常时,请确保展开详细信息并查看内部异常详细信息,因为它将提供有关登录失败原因的详细信息。 In my case the connection string contained a user that did not have access to my database.在我的情况下,连接字符串包含一个无权访问我的数据库的用户。

Regardless of whether you use Integrated Security (the context of the logged in Windows User) or an individual SQL account, make sure that the user has proper access under 'Security' for the database you are trying to access to prevent this issue.无论您是使用集成安全性(登录的 Windows 用户的上下文)还是单个 SQL 帐户,请确保用户在“安全性”下对您尝试访问的数据库具有适当的访问权限,以防止出现此问题。

The SQL Server Express service were not set tostart automatically. SQL Server Express 服务未设置为自动启动。

1) Go to control panel 2) Administrative Tools 3) Service 4) Set SQL Server express to start automatically by clicking on it 5) Right click and start the service 1) 转到控制面板 2) 管理工具 3) 服务 4) 通过单击将 SQL Server express 设置为自动启动 5) 右键单击​​并启动服务

I hope that will help.我希望这会有所帮助。

I had a similar issue with the SQL Server Express Edition on Windows Server 2003 .我对Windows Server 2003上的SQL Server Express Edition也有类似的问题。 I simply added the network service as a user in the database security.我只是将网络服务添加为数据库安全中的用户。

This can also happen if you restore a database and the user already exists with different schema, leaving you unable to assign the correct permissions.如果您恢复数据库并且用户已经存在不同的架构,也会发生这种情况,导致您无法分配正确的权限。

To correct this run:要更正此运行:

USE your_database
EXEC sp_change_users_login 'Auto_Fix', 'user', NULL, 'cf'
GO
EXEC sp_change_users_login 'update_one', 'user', 'user'
GO

I posted a similar issue here, working with a SQL 2012 db hosted on Amazon RDS.我在这里发布了一个类似的问题,使用 Amazon RDS 上托管的 SQL 2012 数据库。 The problem was in the connection string - I had "Application Name" and "App" properties in there.问题出在连接字符串中 - 我在那里有“应用程序名称”“应用程序”属性。 Once I removed those, it worked.一旦我删除了这些,它就起作用了。

Entity Framework 5 and Amazon RDS - "The underlying provider failed on Open." Entity Framework 5 和 Amazon RDS -“底层提供商在打开时失败。”

Make sure that each element value in the connection string being supplied is correct.确保提供的连接字符串中的每个元素值都是正确的。 In my case, I was getting the same error because the name of the catalog (database name) specified in the connection string was incorrect.就我而言,我遇到了同样的错误,因为连接字符串中指定的目录名称(数据库名称)不正确。

我通过重置IIS摆脱了这个问题,但仍然在连接字符串中使用Integrated Authentication

我遇到了同样的问题,但对我有用的是从连接字符串中删除它:

persist security info=True

在服务器计算机上为SQL Server (以及端口 1433)定义新的Windows 防火墙规则可以解决此错误(如果您的服务器名称、用户登录名或密码在您的连接字符串中没有错误...)。

I had a similar error with the inner exception as below:我在内部异常中遇到了类似的错误,如下所示:

operation is not valid for the state of the transaction操作对于事务的状态无效

I could resolve it by enabling DTC security settings.我可以通过启用 DTC 安全设置来解决它。

Go To Properties of DTC, under Security Tab, check the below转到 DTC 的属性,在安全选项卡下,检查以下内容

  • Network DTC Access网络 DTC 访问
  • Allow RemoteClients允许远程客户端
  • Transaction Manager Communication事务管理器通信
  • Allow Inbound允许入站
  • Allow Outbound允许出站

If you happen to get this error on an ASP.NET web application, in addition to other things mentioned check the following:如果您碰巧在 ASP.NET Web 应用程序上遇到此错误,除了提到的其他事项外,请检查以下内容:

  1. Database User Security Permissions (which users are allowed access to your database.数据库用户安全权限(允许哪些用户访问您的数据库。
  2. Check your application pool in IIS and make sure it's the correct one that is allowed access to your database.检查 IIS 中的应用程序池,确保它是允许访问数据库的正确应用程序池。

NONE of the answers worked for me没有一个答案对我有用

I think that some of us all make silly mistakes, there are 100 ways to fail ...我认为我们中的一些人都会犯愚蠢的错误,失败的方式有 100 种......

My issue was new project, I setup all the configuration in another project , but the caller was a Web Api project in which I had to copy the same connection string in the Web api project.我的问题是新项目,我在另一个项目中设置了所有配置,但是调用者是一个 Web Api 项目,我必须在其中复制相同的连接字符串到 Web api 项目中。

I think this is crazy considering I was not even newing up dbcontext or anything from the web api.考虑到我什至没有更新 dbcontext 或来自 web api 的任何东西,我认为这很疯狂。

Otherwise the class library was trying to look for a Database named否则类库试图寻找一个名为的数据库

TokenApi.Core.CalContext   

of which my project is named TokenApi.Core and the CalContext is the name of the connection string and the file name其中我的项目被命名为TokenApi.Core并且CalContext是连接字符串的名称和文件名

I was searching all over the web for this problem.我在网上搜索这个问题。 I had the wrong name in the connection string, please check you connection string in web.config.我在连接字符串中输入了错误的名称,请检查 web.config 中的连接字符串。 I had name="AppTest" but it should have been name="App" .我有name="AppTest"但它应该是name="App"

In my AppTestContext.cs file I had:在我的 AppTestContext.cs 文件中,我有:

public AppTestContext() : this("App") { }

Wrong connection string:错误的连接字符串:

<add connectionString="Data Source=127.0.0.1;Initial Catalog=AppTest;Integrated Security=SSPI;MultipleActiveResultSets=True" name="AppTest" providerName="System.Data.SqlClient" />

Right connection string:右连接字符串:

<add connectionString="Data Source=127.0.0.1;Initial Catalog=AppTest;Integrated Security=SSPI;MultipleActiveResultSets=True" name="App" providerName="System.Data.SqlClient" />

I had a similar issue with exceptions due to the connection state, then I realized I had my domain service class variable marked as static (by mistake).由于连接状态,我遇到了类似的异常问题,然后我意识到我的域服务类变量标记为静态(错误地)。

My guess is that once the service library is loaded into memory, each new call ends up using the same static variable value (domain service instance), causing conflicts via the connection state.我的猜测是,一旦将服务库加载到内存中,每个新调用最终都会使用相同的静态变量值(域服务实例),从而通过连接状态引起冲突。

I think also that each client call resulted in a new thread, so multiple threads accessing the same domain service instance equated to a train wreck.我还认为每个客户端调用都会导致一个新线程,因此访问同一个域服务实例的多个线程等同于火车失事。

我犯的一个常见错误是因为我将应用程序从一台电脑移动到另一台电脑,但上述方法都没有奏效,我忘记将连接字符串复制到 App.Config 和 Web.Config!

I was also facing the same issue.我也面临同样的问题。 Now I have done it by removing the user name and password from the connection string.现在我通过从连接字符串中删除用户名和密码来完成它。

For me it was just a simple mistake:对我来说,这只是一个简单的错误:

I used Amazon EC2 , and I used my elastic IP address in the connection string, but when I changed IP addresses I forgot to update my connection string.我使用了Amazon EC2 ,并在连接字符串中使用了我的弹性 IP 地址,但是当我更改 IP 地址时,我忘记了更新我的连接字符串。

I had this error suddenly happen out of the blue on one of our sites.我在我们的一个网站上突然发生了这个错误。 In my case, it turned out that the SQL user's password had expired!在我的例子中,原来 SQL 用户的密码已经过期了! Unticking the password expiration box in SQL Server Management Studio did the trick!取消选中SQL Server Management Studio中的密码过期框就可以了!

I had a similar problem: In my test-cases executions I always got this error.我有一个类似的问题:在我的测试用例执行中,我总是遇到这个错误。 I found out, that my "Distributed Transaction Service" was not started (run: services.msc -> start "Distributed Transaction Service" (best to set it to start automatic)).我发现我的“分布式事务服务”没有启动(运行:services.msc -> 启动“分布式事务服务”(最好将其设置为自动启动))。 After I did that, it worked like a charm...在我这样做之后,它就像一个魅力......

我将数据库文件(.mdf / .ldf)复制到App_Data文件夹中,以摆脱此异常。

I had the same issue few days ago, using "Integrated Security=True;"几天前我遇到了同样的问题,使用“Integrated Security=True;” in the connection string you need to run the application pool identity under "localsystem" Sure this is not recommended but for testing it does the job.在连接字符串中,您需要在“localsystem”下运行应用程序池标识当然不建议这样做,但为了测试它可以完成这项工作。

This is how you can change the identity in IIS 7: http://www.iis.net/learn/manage/configuring-security/application-pool-identities这是在 IIS 7 中更改身份的方法: http ://www.iis.net/learn/manage/configuring-security/application-pool-identities

IIS中,将应用程序池身份设置为服务帐户用户或管理员帐户或有权对该数据库执行操作的 ant 帐户。

In my case I had a mismatch between the connection string name I was registering in the context's constructor vs the name in my web.config.在我的情况下,我在上下文的构造函数中注册的连接字符串名称与我的 web.config 中的名称不匹配。 Simple mistake caused by copy and paste :D复制和粘贴引起的简单错误:D

    public DataContext()
        : base(nameOrConnectionString: "ConnStringName")
    {
        Database.SetInitializer<DataContext>(null);
    }

我有同样的错误,我发现当我将connectionString更改为新的数据源时,我忘记为新数据库更改用户名和passowrd

I have also had this error happen if the SQL Server Instance name isn't specified and the SQL host has multiple SQL instances installed. 如果未指定SQL Server实例名称,并且SQL主机安装了多个SQL实例,则也会发生此错误。 Here's a couple examples to clarify: 这里有几个例子可以说明:

The connection string below results in the exception "The underlying provider failed on Open" with no inner exception in a .NET WebForms app: 下面的连接字符串导致出现异常“底层提供程序在打开时失败”,而.NET WebForms应用程序中没有内部异常:

 connectionString="metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=Entities;User ID=user;Password=password;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"

The following connection string executes as expected in a .net WebForms app where the SQL environment has multiple instances. 以下连接字符串将在SQL环境具有多个实例的.net WebForms应用程序中按预期执行。 Rare I know, but I have a few different SQL instances on my dev box to accommodate different projects: 很少见,但是我的开发箱上有几个不同的SQL实例可以容纳不同的项目:

 connectionString="metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost\SQLSERVER2014;Initial Catalog=Entities;User ID=user;Password=password;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"

在我的情况下,服务器管理员已更改了服务器地址,因此我不得不将连接字符串更改为新的服务器地址

I had this problem because the Application Pool login this app was running under had changed.我遇到了这个问题,因为运行此应用程序的应用程序池登录已更改。

In IIS:在 IIS 中:

  • Find the Application pool by clicking on your site and going to Basic Settings.通过单击您的站点并转到基本设置来查找应用程序池。

  • Go to Application Pools.转到应用程序池。

  • Click on your site's application pool.单击您站点的应用程序池。

  • Click on Advanced Settings.单击高级设置。

  • In Identity, enter account login and password.在身份中,输入帐户登录名和密码。

  • Restart your site and try again.重新启动您的网站,然后重试。

I have solved this way.我已经通过这种方式解决了。

Step 1: Open Internet Information Service Manager第一步:打开互联网信息服务管理器

Step 2: Click on Application Pools in left navigation tree.第 2 步:单击左侧导航树中的应用程序池。

Step 3: Select your version Pool.第 3 步:选择您的版本池。 In my case, I am using ASP .Net v4.0.就我而言,我使用的是 ASP .Net v4.0。 If you dont have this version, select DefaultAppPool.如果您没有此版本,请选择 DefaultAppPool。

Step 4: Right click on step 3, and select advanced settings.第4步:右键单击第3步,然后选择高级设置。

Step 5: Select Identity in properties window and click the button to change the value.第 5 步:在属性窗口中选择 Identity,然后单击按钮更改值。

Step 6: Select Local System in Built-in accounts combo box and click ok.第 6 步:在内置帐户组合框中选择本地系统,然后单击确定。 That's it.而已。 Now run your application.现在运行您的应用程序。 Everything works well.一切正常。

Codeproject Solution : the-underlying-provider-failed-on-open Codeproject 解决方案:the-underlying-provider-failed-on-open

I get this error when call async EF method from sync Main console ( await was skipped).从同步主控制台调用异步 EF 方法时出现此错误(已跳过await )。

Because async opening a connection was for an already disposed data context.因为异步打开连接是针对已经处理的数据上下文。

Solve: call async EF methods correctly解决:正确调用异步EF方法

You could try to replace the metadata:您可以尝试替换元数据:

metadata=res:// /conString.csdl|res:// /conString.ssdl|res://*/conString.msl

to:至:

metadata=res://*/;

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

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