无法连接到Localdb,但可以使用namedpipe连接

3

我在连接我的应用程序到数据库时非常困难。我尝试使用连接字符串中的(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 Server。

为了测试,我将数据源更改为我的实例的命名管道,并且可以从 (我的应用程序) web config 和 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" />
1个回答

3

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

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/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接