C# 本地数据库连接在Windows和VS更新后退出

C# 本地数据库连接在Windows和VS更新后退出,c#,winforms,C#,Winforms,当前版本: 微软Visual Studio专业版2019 版本16.5.4 我有一个本地桌面清单应用程序,几个月前我用C写的。一切都很好,但今天,Windows update和Visual Studio 2019更新突然出现,所以我继续进行。现在我在连接本地数据库时随机出现错误。这意味着在某些情况下,它可以工作,再次启动应用程序,但它无法连接到数据库 错误: Connection Timeout Expired. The timeout period elapsed while attempt

当前版本: 微软Visual Studio专业版2019 版本16.5.4

我有一个本地桌面清单应用程序,几个月前我用C写的。一切都很好,但今天,Windows update和Visual Studio 2019更新突然出现,所以我继续进行。现在我在连接本地数据库时随机出现错误。这意味着在某些情况下,它可以工作,再次启动应用程序,但它无法连接到数据库

错误:

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;
连接字符串I自动将| DataDirectory |替换为应用程序的路径:

我必须补充一点;连接超时=30到连接字符串的末尾,然后应用程序需要将近20秒才能连接到数据库。DB只有3个表,每个表中的记录不超过40条。以前有人有过这个问题吗?我不会接受需要20秒才能连接到DB的应用程序,DB是我的windows 10计算机的本地应用程序

编辑:我知道会有人问我如何连接。它是在一个更大的方法中创建的,用于访问任何类型的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

听起来LocalDB不适合您的工作。发件人:

连接时,会自动创建并启动必要的SQL Server基础结构

这听起来既昂贵又慢


对于您的小数据集,您可能需要一个进程内数据库引擎,如SQLite。Microsoft的支持生命周期即将结束的进程内数据库名为SQL Server Compact,与SQL Server Express LocalDB不同。

运行process Monitor,查看在延迟期间是否有任何情况发生?每次连接时都会出现延迟。连接后,就没有问题了。第一次发射。这从来都不是问题,代码也没有更改..您是否尝试搜索超时时间已过的错误消息?出现了很多链接,例如:在连接过程中,数据与之有什么关系?当数据超时时,它没有试图检索数据。@开关:我可以远程将相同的信息放入SQL Server,这不是问题。如果SQL Server的远程实例没有提前启动是的,这将是一个问题。我使用的是VisualStudio附带的标准DB,为什么我要安装其他DB,因为VisualStudio是一个资源消耗者。仅仅因为它使用LocalDB进行自己的Intellisense操作,这对您来说并不是一个好的选择。我更改了我的响应。这是在连接期间,因此数据与此无关。另外,在VS升级之后,今天才开始,没有代码更改。数据与它有什么关系?轻量级数据库引擎将解决您的启动时间问题。但是,如果您有15 TB的数据,我不推荐这样做。数据的大小只与加载数据库引擎所受到的相对性能影响有关。对于一个0.5 MB的数据库,加载400 MB的MSSQL引擎代码是一个很大的开销。对于15TB的数据库,它的开销是相同的,但要小得多。
//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