在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
首先连接超时分为三种, 第三种超时比较简单,就是sql语句在数据库中的执行时间。通过 SqlCommand.CommandTimeout就可以进行设置。 第一种超时就是tcp请求的超时,这个是没有办法通过设置属性实现的。但是我们必须要控制它,因为一个连接可能几十秒之后才会回应你超时 http://improve.dk/controlling-sqlconnection-timeouts/ public static class SqlExtensions { public static void QuickOpen(this SqlConnection conn, int timeout) { // We'll use a Stopwatch here for simplicity. A comparison to a stored DateTime.Now value could also be used Stopwatch sw = new Stopwatch(); bool connectSuccess = false; // Try to open the connection, if anything goes wrong, make sure we set connectSuccess = false Thread t = new Thread(delegate() { try { sw.Start(); conn.Open(); connectSuccess = true; } catch { } }); // Make sure it's marked as a background thread so it'll get cleaned up automatically t.IsBackground = true; t.Start(); // Keep trying to join the thread until we either succeed or the timeout value has been exceeded while (timeout > sw.ElapsedMilliseconds) if (t.Join(1)) break; // If we didn't connect successfully, throw an exception if (!connectSuccess) throw new Exception("Timed out while trying to connect."); } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论