SQL Server 登录前握手 [英] SQL Server Pre-Login Handshake
问题描述
我通过 ASP .NET 应用程序连接到 MSSQL 数据库,但有时我在打开连接时遇到此错误.
<块引用>连接超时已过期.尝试使用登录前握手确认时超时时间已过.这可能是因为登录前握手失败或服务器无法及时响应.尝试连接到此服务器所花费的持续时间是 - [Pre-Login] 初始化 = 3;握手=14996;
为了暂时解决它,我必须重新启动 IIS.我正在使用此代码片段连接到 MSSQL:
using (SqlConnection connection = new SqlConnection(connectionString)){连接.打开();/* 我的命令在这里 */连接.关闭();connection.Dispose();SqlConnection.ClearPool(连接);}
我在入站和出站规则中允许端口 1433,但没有更改.当我按照那里的说明进行操作时:
I'm connecting to MSSQL database through my ASP .NET application, but sometimes I got this error while opening connection.
Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=3; handshake=14996;
To solve it temporarily I've to restart IIS. I'm using this code snippet to connect to MSSQL:
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); /* my commands here */ connection.Close(); connection.Dispose(); SqlConnection.ClearPool(connection); }
I allowed the port 1433 in the inbound and outbound rules, but no changes. As I follow the instructions there:
but nothing changed.
解决方案According MSDN Doc .
ClearPool clears the connection pool that is associated with the connection.If additional connections associated with connection are in use at the time of the call, they are marked appropriately and are discarded (instead of being returned to the pool) when Close is called on them.
you should use ClearPool method before connection.close
And If you use Using syntax to manage Object you should't use connection.close method
because it will call when they run to end . just open your connection and execute your command
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlConnection.ClearPool(connection); /* my commands here */ SqlCommand cmd = new SqlCommand("your command",conn); cmd.ExecuteReader() }
refer example : https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
last ensure SQL Server Network Config is right
这篇关于SQL Server 登录前握手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!