10

这个错误让我发疯,我什至无法重现它!

我已将我的网站发布到 Windows Azure 和 SQL Azure,效果很好。突然之间,它不起作用,我收到了这个令人讨厌的错误:

这是我得到的错误:

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

这是错误:

建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:SQL 网络接口,错误: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
{
}
4

2 回答 2

10

我知道这是一个非常古老的帖子,但我只是在同一个问题上作斗争......

我遇到了同样的问题,虽然我不明白,但我发现如果我当前登录(在我的网站上,而不是 Azure 控制台上),我需要注销然后重新登录,这个数据库错误就会消失. 这对我来说毫无意义,但它在我部署后对我有用。我已经部署了很多次并且一切正常(无需注销/登录)。有时它会跳入这种模式。不知道那里发生了什么。

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

于 2013-05-22T19:19:15.617 回答
6

您的应用程序似乎希望能够在 SQL Server Express 的本地实例上使用项目中的附件设置数据库。您的错误消息中的关键行是:

连接字符串使用应用程序 App_Data 目录中的数据库位置指定本地 Sql Server Express 实例。

您可能需要更新配置文件中的连接字符串以匹配 Azure 网站中的连接字符串。使用 SQL Server 设置网站后,可以在 Azure 仪表板中获取此信息,并通过同一仪表板进行设置。

这里有很好的说明:http ://blog.davidebbo.com/2012/09/managing-database-connections-in-azure.html

于 2012-12-05T15:28:57.347 回答