【发布时间】:2017-11-28 06:43:58
【问题描述】:
我有一个在 Visual Studio 中的 IIS express 上运行的 asp.net 应用程序。该应用程序在 IIS express 中运行没有任何问题。但是,当我将项目配置切换为在本地 IIS 上运行时,出现以下数据库错误。
System.Data.SqlClient.SqlException: '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. The specified LocalDB instance does not exist.
这绝对是一个连接问题,因为我尝试连接到远程数据库并且它工作正常。问题是访问本地系统中的数据库。
奇怪的是应用程序在visual studio IIS express上完美运行。
我使用的是windows认证,我的连接字符串如下
<connectionStrings>
<add name="SqlConn" connectionString="Data Source=(localdb)\Projects;Initial Catalog=nga;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient"/>
</connectionStrings>
我尝试根据以下链接修改applicationHost配置
applicationHosts.config 文件的内容
<applicationPools>
<add name="DefaultAppPool" autoStart="true" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated">
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
</add>
<add name="Classic .NET AppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
<add name=".NET v2.0 Classic" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
<add name=".NET v2.0" managedRuntimeVersion="v2.0" />
<add name=".NET v4.5 Classic" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" />
<add name=".NET v4.5" managedRuntimeVersion="v4.0" />
<applicationPoolDefaults managedRuntimeVersion="v4.0">
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="false" />
</applicationPoolDefaults>
</applicationPools>
这没有帮助。我想知道是否有人可以帮助我。
干杯, 瓦伦
【问题讨论】:
-
连接字符串两次?
-
感谢瓦鲁姆。我在这里修复:您的连接字符串错误。首先使用 SQL Server Management Studio (SSMS) 并从登录窗口获取数据库实例。 SSMS 与 SQL Server 一起提供。在连接字符串中使用相同的实例。连接字符串应如下所示:connectionString="Server=myServerName\myInstanceName; Initial Catalog=nga;Integrated Security=True;Connect Timeout=30;"。更改实例名称(来自 SSMS 登录页面)和初始目录。登录后,初始目录应与 SSMS 资源管理器中的数据库名称匹配。
-
我得到了同样的错误。尝试使用
-
我能够成功连接到服务器管理工作室中的数据库。试过这个,它适用于 IIS express 而不是本地 IIS。
-
Integrated Security = true 表示您正在使用 Windows 凭据。所以问题出在本地和远程电脑上的用户帐户上。必须发生两件事。 1)本地和远程PC必须具有相同的组帐户。 2) 用户必须在两台 PC 上添加到组中。通常在公司网络中,它使用组策略。通常用户按部门分组。所以这两个要求都满足了。因此,您需要确保 SQL 数据库已设置为使用正确的 Windows 组帐户,该帐户允许远程 PC 上的用户访问数据库。
标签: c# asp.net sql-server iis iis-express