Can't connect to Localdb but can using namedpipe

China☆狼群 提交于 2019-12-24 09:17:58

问题


I am really suck with connecting my app to a database. I am trying to connect to a database using (localdb)\MSSQLLocalDB in the connection string and I get this error:

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. Cannot create an automatic instance. See the Windows Application event log for error details.
) 

I Can connect to the sql server from SSMS using (localdb)\MSSQLLocalDB

So to test things I changed my datasource to the named pipe of my instance, and I can connect to the db from (my app) web config and SSMS. Only issue with that is that the instance stops after inactivity and when I restart the instance (manually), the named pipe is changed.

Can anyone please help me resolve the issue of not being able to connect using localdb?

Connection strings I am using:

Working:

<add name="constr" connectionString="Data Source=np:\\.\pipe\LOCALDB#FF072C16\tsql\query;Initial Catalog=hands; Integrated Security=True" providerName="System.Data.SqlClient" />

Doesn't work:

<add name="constr" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=hands; Integrated Security=True" providerName="System.Data.SqlClient" />

回答1:


After further investigation I found these errors in the Application even viewer

Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.

Windows API call SHGetKnownFolderPath returned error code: 5. Windows system error message is: Access is denied.
Reported at line: 422. 

Which led me to find this article that solved the issue.

Mainly I had to do these changes:

1. Change the loadUserProfile="true"   to true for the App Pool running the app. 
2. Create a share for my localdb instance and use that in my connection string. 
3. Change the local instance stop time to indefinite
Run the following batch to change the timeout to 65535. This value is in   seconds, but 65535 is the magic number meaning infinite:
exec sp_configure 'user instance timeout',65535
reconfigure
exec sp_configure 'user instance timeout'
go

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

https://blogs.msdn.microsoft.com/sqlexpress/2011/12/08/using-localdb-with-full-iis-part-2-instance-ownership/



来源:https://stackoverflow.com/questions/48296587/cant-connect-to-localdb-but-can-using-namedpipe

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