简体   繁体   中英

Not able to connect to database when run on Local IIS

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

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

在此处输入图片说明

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, eg "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 command sqllocaldb c Projects 11.0 .

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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