在本地IIS上运行时无法连接到数据库

时间:2017-06-25 02:07:27

标签: c# asp.net sql-server iis iis-express

我有一个asp.net应用程序,它运行在Visual Studio的IIS express上。应用程序在IIS Express中运行没有任何问题。但是,当我将项目配置切换为在本地IIS上运行时,我收到以下数据库错误。

System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. 
The server was not found or was not accessible. 
Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 
(provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.

这绝对是一个连接问题,因为我尝试连接到远程数据库并且它有效。问题是访问本地系统中的数据库。

奇怪的是,该应用程序在Visual Studio IIS express上运行完美。

我正在使用Windows身份验证,我的连接字符串如下

<connectionStrings>
    <add name="SqlConn" connectionString="Data Source=(localdb)\Projects;Initial Catalog=nga;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient"/>
</connectionStrings>

我尝试根据以下链接修改applicationHost配置

https://blogs.msdn.microsoft.com/sqlexpress/2011/12/08/using-localdb-with-full-iis-part-1-user-profile/

applicationHosts.config文件的内容

<applicationPools>
   <add name="DefaultAppPool" autoStart="true" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated">
      <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
   </add>
   <add name="Classic .NET AppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
   <add name=".NET v2.0 Classic" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
   <add name=".NET v2.0" managedRuntimeVersion="v2.0" />
   <add name=".NET v4.5 Classic" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" />
   <add name=".NET v4.5" managedRuntimeVersion="v4.0" />
   <applicationPoolDefaults managedRuntimeVersion="v4.0">
      <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="false" />
   </applicationPoolDefaults>
</applicationPools>

这没有帮助。我想知道是否有人可以帮助我。

干杯, VARUN

2 个答案:

答案 0 :(得分:0)

您只需要在SQL Server配置管理器中将“ 全部收听”设置为“是”即可:

enter image description here

答案 1 :(得分:-1)

好。从上面的SqlException开始,它说明了

  

..错误:50 - 发生本地数据库运行时错误。指定的LocalDB实例不存在。

问题必须出在您的连接字符串中,尤其是指向服务器实例的此部分。 Data Source=(localdb)\Projects;。也许你删除它或它不存在。

有两种方法可以调试此问题并找到解决方案:

  1. 构建应用程序后,打开视图&gt;服务器资源管理器或只需使用Ctrl + Alt + S打开Visual Studio左侧的“服务器资源管理器”对话框。在“数据连接”下,检查是否会看到您的型号名称,例如&#34; SqlConn&#34;。 VS将尝试打开与(localdb)的连接。如果失败,那么您可以确定(localdb)\Projects实例已被删除。您可以使用(localdb)\v11.0或使用SqlLocalDB实用程序重新创建Projects实例。
  2. 2.这是SqlLocalDB Utility的链接。使用此命令sqllocaldb c Projects 11.0

    有关此问题的进一步帮助,您可以查看此Question.

    上接受的答案

    希望这有帮助。