连接到SQL Server时出现了与网络或特定实例相关的错误。

53

我将我的asp.net网站部署在somee.com上,每当我登录到此网站(ipc.somee.com)时,它会出现一个网络相关错误,如下所示:

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)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace: 


[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) +5296071
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +558
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5308555
   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) +5311099
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +37
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions) +558
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions) +67
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1052
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +167
   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
   Default.log(Object sender, EventArgs e) in c:\users\sreekanth\documents\visual studio 2010\Projects\IPCWebApp\IPCWebApp\Default.aspx.cs:43
   System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +115
   System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +124
   System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

对我来说,解决方案在给定答案的第8点。除此之外,我在我的应用程序“连接字符串”键值中使用了错误的服务器名称,我必须在连接字符串中更改它。从"Default": "Server=(LocalDb)\\MSSQLLocalDB;Database=TodoApp;Trusted_Connection=True;TrustServerCertificate=True""Default": "Server=localhost;Database=TodoApp;Trusted_Connection=True;TrustServerCertificate=True" - carloswm85
1个回答

65
当你的应用程序没有足够的权限访问数据库时,Sql Server会报错。这个错误有几个可能的原因。为了解决这个错误,你应该按照以下指示进行操作。
  1. 尝试使用管理工具从您的服务器连接到SQL Server。如果您使用Windows身份验证连接到SQL Server,则将应用程序池标识设置为服务器管理员。

  2. 如果您使用SQL Server身份验证,请检查Web应用程序的web.config文件中的连接字符串,并设置允许您登录的SQL Server的用户ID和密码。

  3. 如果您的数据库位于其他服务器上(访问远程数据库),则首先在SQL Server管理工具的SQL Server属性中启用远程访问,并在SQL Server配置管理器中启用TCP/IP。

  4. 在完成所有这些操作后,如果仍然无法访问数据库,请检查您尝试访问数据库的服务器的防火墙,并在防火墙中添加一个规则以启用SQL Server的端口(默认情况下,SQL Server使用1433端口,要检查SQL Server的端口,您需要检查SQL Server配置管理器网络协议TCP/IP端口)。

  5. 如果您的SQL Server运行在命名实例上,则需要在SQL Server名称后面写上端口号,例如117.312.21.21/nameofsqlserver,1433。

  6. 如果您正在使用像Amazon AWS或Microsoft Azure这样的云托管,则服务器或实例将在云防火墙后运行,因此如果您有默认实例或特定端口用于SQL Server的命名实例,则需要在云防火墙中启用1433端口。

  7. 如果您正在使用Amazon RDS或SQL Azure,则需要从该实例的安全组中启用端口。

  8. 如果您通过SQL Server身份验证模式访问SQL Server,请确保已启用“SQL Server和Windows身份验证模式”SQL Server实例属性。 enter image description here

  9. 在更改属性后,请重新启动SQL Server实例,因为某些更改需要重新启动。

如果你还遇到任何困难,那么你需要提供关于你的网站和SQL服务器的更多信息。

4
我遇到了这个问题,但之前我曾经连接过一次,现在从asp.net无法连接。连接字符串是正确的。我认为这可能与实体框架有关,仍在调查中,不确定是否是上述问题的原因。 - Jason Foglia
对我来说,这绝对是一个权限问题。asp.net 正在尝试创建它自己的数据库,在 godaddy 的共享主机上,你只能通过用户界面创建数据库。 - Jason Foglia
1
您无法从asp.net应用程序创建数据库,因为您用于连接到SQL服务器的用户ID和密码没有该权限。如果您想创建新数据库,则只能使用Godaddy Web界面。 - Hiren Dhaduk
1
我也遇到了这个错误,但我并没有尝试创建任何表格或新列。它会工作一分钟,然后当我尝试更改并保存时,我会遇到这个错误大约一个小时左右。 - Chris
4
虽然这个问题已经关闭,但问题和答案都很特别。特别是web.config中的 #5(端口号)对帮助我解决问题有巨大的帮助! - JosephDoggie
显示剩余6条评论

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