Azure网站无法访问Azure数据库。

10

这个错误让我发疯,我甚至无法复现它!

我已经将我的网站发布到Windows Azure和SQL Azure,并且一切都运行得很好。突然之间它就不能工作了,我收到了这个令人讨厌的错误:

这是我收到的错误:

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

这是错误信息:

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

我想强调的是,我可以通过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 Id=shimmy@tcp: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
{
}

很奇怪。你能远程连接实例并检查web.config文件,确保你使用的是正确的连接字符串吗? - Win
我对此还不太熟悉,您如何远程桌面连接到Azure服务器? - Shimmy Weitzhandler
http://www.windowsazure.com/en-us/develop/net/common-tasks/remote-desktop/ - Win
2个回答

10

我知道这是一篇非常旧的文章,但我刚刚遇到了同样的问题...

我也遇到了同样的问题,虽然我不理解它,但我发现如果我当前已登录(我的网站上,而不是Azure控制台),我需要注销然后重新登录,这样数据库错误就会消失。对我来说,这是有效的方法,可以在部署后使用。有很多次我部署后一切正常(不需要注销/登录)。有时候会出现这种情况,不确定那里出了问题。

这与web.config中的连接字符串无关 - 它们都被正确转换了。


谢谢@CPank,通过重新启动浏览器解决了问题,就像你描述的那样。 - hawbsl
1
你真是太棒了!我都快抓狂了,然后读了你的帖子之后,我只是清除了一下我的cookies,问题就解决了!感谢您,先生。 - N1njaB0b

6
看起来您的应用程序希望能够在本地 SQL Server Express 实例中使用项目中的附加文件设置数据库。您错误消息中的关键行是:
连接字符串指定了一个本地 Sql Server Express 实例,该实例使用应用程序的 App_Data 目录中的数据库位置。
您可能需要更新配置文件中的连接字符串以匹配 Azure 网站中的连接字符串。此信息可以在设置带有 SQL Server 的网站后在 Azure 仪表板中获取,并通过同一仪表板进行设置。
这里有一个很好的指南:http://blog.davidebbo.com/2012/09/managing-database-connections-in-azure.html

请检查您的 web.config.cloud 文件,并确保您在其中指定了正确的连接字符串或转换。 - Chris Koenig
我已经检查了服务器的web.configuration中的连接字符串,那里没有提及与App_Data的连接... - Shimmy Weitzhandler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接