SQL连接错误

问题描述:

我收到此错误试图访问我的网站:SQL连接错误

Exception information: 

Exception type: SqlException 

Exception message: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

这个错误在一整天发生6-7次。

有谁能告诉我为什么我得到这个错误?有什么特殊的设置可以在我的SQL服务器端进行,以避免在我的Web应用程序中出现这种崩溃或问题?

+2

如果您发布连接字符串(没有密码/用户名称of course),这将会有所帮助。 – JConstantine

+1

如果您的连接通常有效并且有时不起作用,那么我可以考虑网络问题(例如大流量)或高负载在服务器上,它不会响应新的连接尝试。 – Marco

+0

可能是您的网站正在循环访问数据库,导致数据库非常繁忙,以至于它在一段时间内不可用,并引发此类错误。为此,您应该查看db – Badr

这里是如何在SQL Server 2005中配置为允许远程连接:http://support.microsoft.com/kb/914277

这可以通过各种原因引起:在SQL Server中禁用

  • 远程连接。
  • 防火墙设置
  • 坏连接字符串

微软的这种KB-文章可以帮助... http://support.microsoft.com/default.aspx?scid=kb;EN-US;914277

编辑:请注意,如果你有一个本地数据库的系统上进行开发数据通常通过共享内存进行交换。在测试和生产环境中,您有一个单独的数据库服务器,必须通过tcp协议与您的应用程序连接。

的ConnectionString:

data source=[SERVERADDRESS];initial catalog=[DBNAME];user id=[USER];password=[PASSWORD];persist security info=True;Pooling=True;Max Pool Size=1000;Connection Lifetime=30;

你真的必须设置池的大小和连接一辈子?我很少使用这个设置。他们可能会造成麻烦。特别是

Connection Lifetime

应该小心使用。在这里阅读更多信息http://blogs.msdn.com/b/angelsb/archive/2004/09/20/231963.aspx

如果此问题只是偶尔出现,那么很可能您正在运行一个(或多个)命令的超时。

尝试增加命令的CommandTimeout属性的值。

也尝试分析您的Sql服务器的长时间查询。