Current version: Microsoft Visual Studio Professional 2019 Version 16.5.4
I have a local desktop inventory app, I wrote in C# a few months ago. Everything has been just fine, but today, Windows update and Visual Studio 2019 update popped up so I went ahead. Now I "RANDOMLY" get errors connecting to the local DB. Meaning some times it works, launch the app again and it fails to connect to the DB.
Error:
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;
Connection String (I auto replace the |DataDirectory| with the path of the application):
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MYDB.mdf;Integrated Security=True
I have to add ";Connect Timeout=30" to the end of the connection string, then the app takes almost 20 seconds to connect to the DB. The DB only has 3 tables, with no more than 40 records in each table. Anyone else have this issue before? I'm not going to accept the application taking 20 seconds to connect to the DB, which is local to my windows 10 machine.
EDIT: I know there will be people asking how am I connecting. It's in a larger method created for accessing any kind of MS DB.
//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