无法连接到Localdb,但可以使用namedpipe [英] Can't connect to Localdb but can using namedpipe

查看:242
本文介绍了无法连接到Localdb,但可以使用namedpipe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

可以使用(localdb)\ MSSQLLocalDB

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

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.

任何人都可以帮助我解决无法使用localdb进行连接的问题吗?

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

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

Connection strings I am using:

工作:

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

推荐答案

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

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

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆