8

這個錯誤使我瘋狂,我甚至不能重現它!Azure網站無法訪問Azure數據庫

我已經將我的網站發佈到Windows Azure和SQL Azure,它運行得非常好。 突然之間,它不工作,我得到這個討厭的錯誤:

以下是錯誤我得到:

http://jsfiddle.net/shimmy/pcS7g/embedded/result

這裏的錯誤:

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: 26 - Error Locating Server/Instance Specified)

我想強調我可以通過SSMS或VS訪問SQL並查看我的表等。

我不知道是什麼原因導致了這個惱人的錯誤在每次部署後都會一次又一次發生。

堆棧跟蹤:

[SqlException (0x80131904): 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: 26 - Error Locating Server/Instance Specified)] 
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295167 
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242 
    System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5307115 
    System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145 
    System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +920 
    System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307 
    System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434 
    System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +5309659 
    System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38 
    System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +5311874 
    System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +143 
    System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +83 
    System.Data.SqlClient.SqlConnection.Open() +96 
    System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +76 

[HttpException (0x80004005): Unable to connect to SQL Server database.] 
    System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +131 
    System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +89 
    System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +27 
    System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +386 

更新

這裏是連接字符串的副本,因爲它們出現在服務器上的web.config:

<connectionStrings> 
    <add name="Context" connectionString="Data Source=tcp:abcdefg.database.windows.net,1433;Initial Catalog=Database;User [email protected]:abcdefg;Password=my123password;" providerName="System.Data.SqlClient" /> 
    <add name="Storage" connectionString="DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accountkey==" /> 
</connectionStrings> 

我甚至試過完全從服務器的web.config中刪除連接字符串。我不知道這個App_Data是從哪裏來的!

DbContext看起來是這樣的:

public Context() 
    : base("name=Context") //I also tried "Context" alone 
{ 
} 
+0

這很奇怪。你可以rdp的實例,並檢查web.config,以確保您使用正確的連接字符串? – Win

+0

我對此很新,你如何能夠使用Azure服務器? – Shimmy

+0

http://www.windowsazure.com/en-us/develop/net/common-tasks/remote-desktop/ – Win

回答

6

看來,你的應用程序被期待能夠建立使用連接的文件數據庫在你的項目上的SQL Server Express的本地實例。在錯誤消息中的關鍵行是:

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory.

你可能需要在您的配置文件以匹配您Azure的網站連接字符串更新連接字符串。一旦您使用SQL Server設置網站,就可以在Azure儀表板中獲取此信息,並通過相同的儀表板進行設置。

下面是這樣做很好的說明:http://blog.davidebbo.com/2012/09/managing-database-connections-in-azure.html

+0

檢查你的web.config.cloud,並確保你有正確的連接字符串或在那裏指定的轉換 –

+0

我檢查了服務器的web.configuration上的連接字符串,並且沒有提及與App_Data的連接... – Shimmy

8

我知道這是一個非常古老的職位,但我只用了同樣的問題...作戰

我有同樣的問題,雖然我不理解它,我發現如果我當前登錄(在我的網站上,而不是Azure控制檯),我需要註銷然後再次登錄,並且此數據庫錯誤消失。這對我來說沒有意義,但在部署後它對我來說很有用。我已經有很多次部署,並且一切都很好(不需要註銷/登錄)。有時它跳入這種模式。不知道那裏發生了什麼。

它與web.config中的連接字符串無關 - 它們都被正確轉換。

+0

感謝@CPank,通過重新啓動瀏覽器進行修復,就像您所描述的一樣 – hawbsl

+1

您是男人!我正在拉我的頭髮,然後看完你的帖子,我只是清除了我的餅乾,問題就消失了!祝福你,先生。 – N1njaB0b

相關問題