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

相关问题