我真的很难将我的应用程序连接到数据库。我正在尝试使用连接字符串中的(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配置和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" />
答案 0 :(得分:1)
经过进一步调查后,我在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.
这让我找到了解决这个问题的文章。
主要是我必须做这些改变:
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