2012-05-02 29 views
10

我使用ASP.Net MVC 3(實體框架)和Sql server 2008.連接字符串在web的.config。但有時候,我從我的應用程序出現以下情況例外:與SQL Server建立連接時發生網絡相關或特定於實例的錯誤 - ASP.NET MVC實體框架

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, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) 
    at System.Data.SqlClient.SqlInternalConnectionTds.LoginWithFailover(Boolean useFailoverHost, ServerInfo primaryServerInfo, String failoverHost, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) 
    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) 
    at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, 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, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, 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> 

此異常抽到/每次重現。任何可能的設置我可能會在這裏失蹤?請建議。

+0

出現此錯誤時,是您在網絡或電腦沒有?我想你應該先檢查你的局域網或互聯網連接。 –

+0

在本地沒有發生,我只從部署的服務器獲取此異常。 – Prasad

+0

@Prasad檢查我提供的連接字符串.... –

回答

2

鑑於這一事實,有時/基本能工作(如你所說):

  • 連接字符串OK
  • SQL配置權(即允許遠程連接等)

這可能的唯一原因是您的服務器(您的代碼)和SQL Server之間的網絡丟失,或者SQL Server因某種原因而被關閉。

+0

當我從那裏得到這個異常的同一頁面時,它工作,所以服務器關機可能不是理由。而且網站和sql server都在同一臺服務器上,所以我不確定網絡是否會丟失可能是一個原因。 – Prasad

+0

[Googling](https://www.google.com/search?sourceid=chrome&ie=UTF-8&q=A+network-related+or+instance-specific+error+occurred+while+establishing+a+connection+to + SQL + Server)這個錯誤提供了很多可能的解決方案。你過了他們嗎?另外,具體來說,請運行'ping -t localhost'(或您的SQL機器名稱),讓它運行一段時間,看看您是否會不時丟失數據包。 –

+0

我已經嘗試從谷歌的這些選項,所有給出的選項都配置正確,但仍然得到這個異常:(。我也檢查了平,我有沒有任何失敗的答覆 – Prasad

0

試試這個....

<add name="MyAppDBContainer" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string='data source=SERVERNAME;initial catalog=MyAppDB;persist security info=True;user id=USERID;Password=PASSWORD;multipleactiveresultsets=True;'" providerName="System.Data.EntityClient" /> 

我已經編輯我的答案.....

+0

它給出了錯誤:System.ArgumentException:連接字符串中缺少一些必需的信息。'provider'關鍵字始終是必需的 – Prasad

+0

naim,我已將連接字符串更改爲更新的連接字符串,並且仍然會引發連接錯誤有時候是 – Prasad

+0

!,所以它會工作一段時間,有時候不會... –

0

我們有檢查以下事情

1.檢查sqlserver配置管理器設置中的Tcp/IP設置。如果禁用,make sure we have to enable the status for TCP/IP

2.添加端口到防火設置的防火牆例外。 詳細的SQL端口:

名稱:SQL

端口號:1433

協議:選擇TCP

相關問題