【问题标题】:SQL Timeout is longer than I set itSQL Timeout 比我设置的要长
【发布时间】:2018-11-29 08:25:40
【问题描述】:

我在 C# 中使用 SqlConnection,并打开与 sqlconnection.Open() 的连接到一个 VM,MSSQL 在其上运行。如果凭据正确,则连接可用不到一秒钟。但如果例如数据库不可用,它会导致大约 45 秒的长时间超时。

这就是我添加到连接字符串Connection Timeout={Timeout} 的原因,这是因为sqlConnection.ConnectionTimeout 等于我设置的超时。

我将超时设置为 5 秒并尝试连接到不存在的数据库。不幸的是,超时时间比我预期的要长。

20/06/2018 08:04:39:打开连接

20/06/2018 08:05:11:连接失败

我的目标是在 ca 之后断开连接。 10 秒。

TL;DR;我可以影响超时吗?如果这是不可能的, 为什么超时时间约为 32 秒。即使我将超时设置为 5 秒。

编辑:

connectionstring = $@"Server={Ip};Database={DbName};User Id={Username};Password={Password};Connection Timeout={Timeout}";
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{ 
    try {
        Debug.WriteLine(DateTime.Now + "Open Connection");
        sqlConnection.Open();
    }
    catch
    {
        Debug.WriteLine(DateTime.Now + "Connection failed");
    }
}

编辑 2:

我在连接字符串中添加了ConnectRetryCount=0;ConnectRetryInterval=1;,它没有做任何改变。

【问题讨论】:

标签: c# sql sql-server-2008 timeout sqlconnection


【解决方案1】:

https://stackoverflow.com/a/50944424/5507322 - 公平点,但并非在所有情况下都有效。

上面对此的解释,为什么它不可靠以及我的问题的解决方案: http://improve.dk/controlling-sqlconnection-timeouts/

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2014-11-05
    相关资源
    最近更新 更多