Microsoft Visual Studio专业版2019
版本16.5.4
我有一个本地桌面库存应用程序,几个月前我用C写的。一切都很好,但是今天,Windows更新和visualstudio2019更新突然出现,所以我继续。现在我“随机”在连接到本地数据库时出错。这意味着有时它能工作,再次启动应用程序,它无法连接到数据库。
错误:
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=16063; handshake=8123;
我自动将| DataDirectory |替换为应用程序的路径
):
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MYDB.mdf;Integrated Security=True
我必须在连接字符串的末尾添加“;Connect Timeout=30”,然后应用程序几乎需要20秒才能连接到数据库。数据库只有3个表,每个表中的记录不超过40条。其他人以前也有这个问题吗?我不会接受这个应用程序需要20秒才能连接到数据库,它是我的Windows10机器的本地服务器。
//set connection
SqlConnection connection = new SqlConnection(ConnectionString);
//set command
SqlCommand command = new SqlCommand()
{
Connection = connection,
//even though it's set to procedure, the Microsoft.Security
//will still have warnings, because the proc name is passed
//in, instead of a constant.
CommandText = storedProcedure,
CommandTimeout = CommandTimeout
};
//if the proc has parameters, pass them.
if (param != null)
{
command.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < param.Length; i++)
{
param[i].ParameterName = Clean(param[i].ParameterName, CleanType.UnquotedString);
if(param[i].DbType == DbType.String)
param[i].Value = Clean(param[i].Value.ToString(), CleanType.UnquotedString);
command.Parameters.Add(param[i]);
}
}
else
command.CommandType = CommandType.Text;
//open connection
connection.Open(); //<---- This is what Times Out