我通过ASP .NET应用程序连接到MSSQL数据库,但有时在打开连接时出现此错误。
连接超时已过期。尝试使用登录前握手确认时超时时间已过。这可能是因为登录前握手失败或服务器无法及时响应。尝试连接到此服务器所花费的时间是 - [Pre-Login] initialization = 3;握手= 14996;
暂时解决它我要重启IIS。我正在使用此代码段连接到MSSQL:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
/* my commands here */
connection.Close();
connection.Dispose();
SqlConnection.ClearPool(connection);
}
我在入站和出站规则中允许端口1433,但没有更改。当我按照那里的指示:
但没有改变。
答案 0 :(得分:3)
根据MSDN Doc。
ClearPool清除与该连接关联的连接池。如果在呼叫时正在使用与连接关联的其他连接,则它们会被正确标记并在关闭时被丢弃(而不是返回到池中)拜访了他们。
你应该在connection.close之前使用ClearPool方法
如果使用Using语法来管理Object,则不应使用connection.close方法
因为它们会在结束时调用。只需打开您的连接并执行命令
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlConnection.ClearPool(connection);
/* my commands here */
SqlCommand cmd = new SqlCommand("your command",conn);
cmd.ExecuteReader()
}
最后确保SQL Server网络配置正确
答案 1 :(得分:1)
对我来说,诀窍是增加连接字符串的超时,因为当通过vpn连接时,建立连接花了很长时间。您可以通过添加; connection timeout = value
来完成此操作当我在vpn上连接尝试连接到sql server的应用程序时,我遇到了同样的错误。
默认情况下,超时设置为15秒。
希望它有所帮助!