无法连接到 Localdb 但可以使用命名管道

Can't connect to Localdb but can using namedpipe

我真的很讨厌将我的应用程序连接到数据库。我正在尝试在连接字符串中使用 (localdb)\MSSQLLocalDB 连接到数据库,但出现此错误:

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

可以 使用 (localdb)\MSSQLLocalDB

从 SSMS 连接到 sql 服务器

所以为了测试,我将数据源更改为实例的命名管道,并且可以从(我的应用程序)Web 配置和 SSMS 连接到数据库。唯一的问题是实例在不活动后停止,当我(手动)重新启动实例时,命名管道被更改。

谁能帮我解决无法使用 localdb 连接的问题?

我正在使用的连接字符串:

工作:

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

无效:

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

经过进一步调查,我在应用程序甚至查看器中发现了这些错误

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. 

这让我找到了这篇解决问题的文章。

主要是我必须做这些改变:

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/