Not able to connect to database when run on Local IIS

孤者浪人 提交于 2020-12-07 10:26:52

问题


I have an asp.net application which runs on IIS express in visual studio. The application runs without any problems in IIS express. However when I switch the project configuration to run on Local IIS, I get the following database error.

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.

This is definitely a connectivity problem as I tried connecting to a remote database and it worked. The problem is accessing a database in the local system.

The strange thing is that the application runs perfectly on visual studio IIS express.

I am using windows authentication and my connection string is as follows

<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>

I tried modifying the applicationHost configuration according to the following link

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

Contents of applicationHosts.config file

<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>

That did not help. I was wondering if anyone out there could help me.

Cheers, Varun


回答1:


Okay. From the SqlException above, it states that

..error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.

The problem must be in your connection string especially this part where you point to your server instance. Data Source=(localdb)\Projects;. Maybe you deleted it or it does not exist.

There are two ways to debug this problem and arrive at a solution:

  1. After you build your application, Open View > Server Explorer or simply use Ctrl+Alt+S to open the Server Explorer dialog on the left side of Visual Studio. Under Data Connections, check if you will see your model name, e.g "SqlConn". VS will try to open a connection to (localdb). If it fails, then you can be sure that (localdb)\Projects instance got deleted. You can either use (localdb)\v11.0 or recreate Projects instance using SqlLocalDB utility.

2.Here is a link to SqlLocalDB Utility. Use this commandsqllocaldb c Projects 11.0.

For further assistance on this problem, you can check with the accepted answer on this Question.

Hope this helps.




回答2:


You just need to set "Listen All" to yes in the SQL Server Configuration Manager:



来源:https://stackoverflow.com/questions/44742555/not-able-to-connect-to-database-when-run-on-local-iis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!