有时会"A network-related or instance-specific error occurred while establishing a connection to SQL Ser



我不时会收到

在建立与SQL Server的连接时发生与网络相关或特定于实例的错误。

在细节中显示

SQL Server Express数据库文件自动创建错误

此错误随机出现。有时,当我转到另一个页面时,它加载得很好,当我回到/Home/Index时,错误就会消失。

我在本地主机上使用Asp.NetMVC5。

我在web.config:中只有一个DefaultConnection

<connectionStrings>
<!--<clear />-->
<add name="DefaultConnection" 
connectionString="Data Source=(LocalDb)v11.0;AttachDbFilename=|DataDirectory|RadiologyManager.mdf;Initial Catalog=RadiologyManager;Integrated Security=True" 
providerName="System.Data.SqlClient" />
</connectionStrings>

如前所述,我尝试添加clear,但它给出了另一个错误,即找不到来自machine.config的连接。

错误

在建立与SQL Server的连接。找不到服务器或无法访问。验证实例名称是否正确,以及SQL Server已配置为允许远程连接。(提供程序:SQL网络接口,错误:26-定位服务器/实例时出错指定)

描述:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误以及错误在代码中的来源的更多信息。

SQL Server Express数据库文件自动创建错误:

连接字符串使用应用程序的App_Data目录中的数据库位置指定本地Sql Server Express实例。提供程序尝试自动创建应用程序服务数据库,因为提供程序确定该数据库不存在。以下配置要求对于成功检查应用程序服务数据库的存在并自动创建应用程序服务数据库是必要的:

如果应用程序在Windows 7或Windows Server 2008R2上运行,则需要执行特殊的配置步骤才能自动创建提供程序数据库。更多信息请访问:http://go.microsoft.com/fwlink/?LinkId=160102.如果应用程序的App_Data目录不存在,则web服务器帐户必须具有对应用程序目录的读写访问权限。这是必要的,因为如果App_Data目录不存在,web服务器帐户将自动创建该目录。

如果应用程序的App_Data目录已经存在,则web服务器帐户只需要对应用程序的App_Data目录进行读写访问。这是必要的,因为web服务器帐户将尝试验证Sql server Express数据库是否已存在于应用程序的App_Data目录中。从web服务器帐户吊销对App_Data目录的读取访问权限将使提供程序无法正确确定Sql server Express数据库是否已存在。当提供程序尝试创建已存在数据库的副本时,这将导致错误。需要写入访问权限,因为在创建新数据库时会使用web服务器帐户的凭据。

必须在计算机上安装Sql Server Express。

web服务器帐户的进程标识必须具有本地用户配置文件。有关如何为计算机帐户和域帐户创建本地用户配置文件的详细信息,请参阅自述文档。

堆栈跟踪

[SqlException(0x80131904):在与SQL Server建立连接时发生与网络相关或实例特定的错误。找不到服务器或无法访问服务器。请验证实例名称是否正确,以及SQL Server是否已配置为允许远程连接。(提供程序:SQL网络接口,错误:26-定位指定的服务器/实例时出错)]

System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection,操作`1 wrapCloseInAction)+5341687

System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用程序HasConnectionLock,布尔异步关闭)+546
System.Data.SqlClient.TdssParser.Connect(ServerInfo ServerInfo,SqlInternalConnection Tds connHandler,布尔忽略SniOpenTimeout,Int64 timerExpire,布尔加密,布尔信任ServerCert,布尔集成安全,布尔带故障转移)+5353471
System.Data.SqlClient.SqlInternalConnectionTds.AttremptOneLogin(ServerInfo ServerInfo,String newPassword,SecureString newSecurePassword,布尔值ignoreSniOpenTimeout,TimeoutTimer timeout,布尔值withFailover)+145
System.Data.SqlClient.SqlInternalConnectionTds.LoginoFailover(ServerInfo ServerInfo,String newPassword,SecureString newSecurePassword,布尔重定向用户实例,SqlConnectionString连接选项,SqlCredential凭据,超时计时器超时)+923
System.Data.SqlClient.SqlInternalConnection Tds.OpenLoginList(TimeoutTimer超时,SqlConnectionString connectionOptions,SqlCredential凭据,String newPassword,SecureString newSecurePassword,布尔重定向UserInstance)+311
System.Data.SqlClient.SqlInternalConnectionTds.ctor(DbConnectionPoolIdentity标识、SqlConnectionString连接选项、SqlCredential凭据、对象提供程序信息、字符串newPassword、SecureString newSecurePassword、布尔重定向的UserInstance、SqlConnectionString-userConnectionOptions、SessionData reconnectionSessionData)+646
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection,DbConnectionOptions userOptions)+5536769
System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owning Connection,DbConnection poolGroup poolGroup,DbConnectionOptions userOptions)+38
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +5358970
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1重试,DbConnectionOptions userOptions)+146
System.Data.ProviderAbase.DbConnectionClosed.TryOpenConnection(DbConnectionouterConnection,DbConnectionFactory connectionFactory,TaskCompleteSource1 retry, DbConnectionOptions userOptions) +16
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource
1重试)+94
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1重试)+110
System.Data.SqlClient.SqlConnection.Open()+96
System.Web.Management.SqlServices.GetSqlConnection(字符串服务器,字符串用户,字符串密码,布尔可信,字符串连接字符串)+75

[HttpException(0x80004005):无法连接到SQL Server数据库。]
System.Web.Management.SqlServices.GetSqlConnection(字符串服务器,字符串用户,字符串密码,布尔可信,字符串连接字符串)+130
System.Web.Management.SqlServices.SetupApplicationServices+89
System.Web.Management.SqlServices.Install(字符串数据库,字符串dbFileName,字符串连接字符串)+27
System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(字符串fullFileName,串数据目录,字符串连接串)+386

~编辑~
Web.Config

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<connectionStrings>
<!--<clear />-->
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)v11.0;Initial Catalog=RadiologyManager;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<roleManager enabled="true" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

更新(已解决)

根据Aaron Bertrand的建议,我在Management Studio中安装了SQL Server Express 2014。

SQL Server 2008和2012在安装过程中一直无法安装。经过两天的努力,我成功安装了2012,但未能加载。因此,我使用了带有Management Studio的SQL Server Express 2014,它的安装没有任何问题,运行良好。到目前为止还不错。

按照以下更新web.config中的连接

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=MYSYSSQLEXPRESS;Initial Catalog=RadiologyData;User ID=sa;Pooling=False;Password=abc123" providerName="System.Data.SqlClient" />
</connectionStrings>

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">-->
<parameters>
<parameter value="Data Source=MYSYSSQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
<!--<parameter value="mssqllocaldb" />-->
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

您是否使用SQL Server Express进行调试:您是唯一一个连接到数据库的人吗?SQL Server Express的最大连接数。您需要查看SQL Server Express的特定版本,以验证它的最大值。

如果这只是你访问网站:你是否验证了你的连接已全部关闭?

以下是一份关于连接过多导致超时的报告。它也有一些研究链接。

https://dba.stackexchange.com/questions/31173/max-connections-sql-server-express-could-have

我通过将其添加到connecton字符串来解决问题。

Connection Timeout=60;

时间是15秒,这就是为什么我有时会出现这个错误。

https://blogs.msdn.microsoft.com/docast/2018/10/11/connection-timeout-and-command-timeout-in-sql-server/

连接超时不是命令暂停。。。

首先,检查应该允许的远程连接。然后,检查所有与防火墙相关的设置。我花了两个小时在同一个问题上,但有一个愚蠢的解决方案。

1.我刚刚清除了chrome浏览器的缓存/历史记录,并解决了我的问题

2.在另一个浏览器中运行到您的应用程序

相关内容