asp.net-mvc-3 – 与SQL Server建立连接时发生与网络相关或实例特定的错误 – ASP.NET MVC实体框架

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 与SQL Server建立连接时发生与网络相关或实例特定的错误 – ASP.NET MVC实体框架前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用ASP.Net MVC 3(实体框架)与sql server 2008.连接字符串在web.config中很好.但有时我从我的应用程序得到以下异常:
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: Named Pipes Provider,error: 40 - Could not open a connection to sql Server)

Generated: Wed,02 May 2012 05:05:21 GMT

System.Data.EntityException: The underlying provider Failed on Open. ---> 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: Named Pipes Provider,error: 40 - Could not open a connection to sql Server)
   at System.Data.sqlClient.sqlInternalConnection.OnError(sqlException exception,Boolean breakConnection)
   at System.Data.sqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.sqlClient.TdsParser.Connect(ServerInfo serverInfo,sqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity)
   at System.Data.sqlClient.sqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,TimeoutTimer timeout,sqlConnection owningObject)
   at System.Data.sqlClient.sqlInternalConnectionTds.LoginWithFailover(Boolean useFailoverHost,ServerInfo primaryServerInfo,String failoverHost,Boolean redirectedUserInstance,sqlConnection owningObject,sqlConnectionString connectionOptions,TimeoutTimer timeout)
   at System.Data.sqlClient.sqlInternalConnectionTds.OpenLoginEnlist(sqlConnection owningObject,Boolean redirectedUserInstance)
   at System.Data.sqlClient.sqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,Object providerInfo,Boolean redirectedUserInstance)
   at System.Data.sqlClient.sqlConnectionFactory.CreateConnection(DbConnectionOptions options,Object poolGroupProviderInfo,DbConnectionPool pool,DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection,DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory)
   at System.Data.sqlClient.sqlConnection.Open()
   at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition,DbConnection storeConnectionToOpen,DbConnection originalConnection,String exceptionCode,String attemptedOperation,Boolean& closeStoreConnectionOnFailure)
   --- End of inner exception stack trace ---
   at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition,Boolean& closeStoreConnectionOnFailure)
   at System.Data.EntityClient.EntityConnection.Open()
   at System.Data.Objects.ObjectContext.EnsureConnection()
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
   at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query,Expression queryRoot)
   at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
   at MyApp.Data.UserRepository.GetUserDetails(String username)
   at MyApp.Core.Services.SiteService.GetUserDetails(String username,Int64 userId)
   at MyApp.PublicSite.Web.Controllers.BaseController.Initialize(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at MyApp.PublicSite.Web.Controllers.BaseController.Execute(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
   at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
   at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously)

连接字符串是:

<connectionStrings>
    <add name="MyAppDBContainer" connectionString="Metadata=res://*/MyAppDB.csdl|res://*/MyAppDB.ssdl|res://*/MyAppDB.msl;provider=System.Data.sqlClient;provider connection string=&quot;data source=SERVERNAME;initial catalog=MyAppDB;persist security info=True;user id=USERID;Password=PASSWORD;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

这个异常是随机发生的,不能每次得到/再生.任何可能的设置,我可能会在这里丢失?请建议.

解决方法

鉴于有时/主要工作(正如你所说):

>连接字符串OK
> sql配置正确(即允许远程连接等)

唯一的原因可能是您的服务器(您的代码)和sql Server之间的网络丢失,或者sql Server由于某些原因而关闭.

猜你在找的asp.Net相关文章