C# MySqlException:超时已过期 - 增加连接超时没有影响
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11509817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySqlException: Timeout expired - Increasing Connection Timeout Has Had No Effect
提问by Lonnie Best
I have a query that is taking longer to execute as the database increases in size. The query is optimized and necessary, but my C# Console Application has recently been giving me this error:
随着数据库大小的增加,我有一个查询需要更长的时间来执行。查询经过优化和必要,但我的 C# 控制台应用程序最近给了我这个错误:
Unhandled Exception: MySql.Data.MySqlClient.MySqlException: Timeout expired.
Increasing the connection time out in the connection string doesn't help; I increased it from
增加连接字符串中的连接超时没有帮助;我从
Connect Timeout=28800
连接超时=28800
to
到
Connect Timeout=128800
连接超时=128800
but I'm still getting the error despite this change.
但尽管发生了这种变化,我仍然收到错误消息。
If I run the query from MySQL workbench it only take about 10 seconds, so I'm not sure how to prevent this Unhandled Exception.
如果我从 MySQL 工作台运行查询,它只需要大约 10 秒,所以我不确定如何防止这个未处理的异常。
Are there other things, besides "the time a query takes", that can produce this exception?
除了“查询所需的时间”之外,还有其他事情会产生此异常吗?
采纳答案by Dragonalighted
I've had this problem before. ConnectTimeout property only applies to time outs that occur when connecting to the database, not for queries.
我以前遇到过这个问题。ConnectTimeout 属性仅适用于连接到数据库时发生的超时,不适用于查询。
CommandTimeouthowever specifies how long it should wait for the query to return. I believe the default is 30 seconds. Double check the documentation for your MySql library, but for SqlCommand the CommandTimeout is in Seconds not milliseconds.
然而,CommandTimeout指定等待查询返回的时间。我相信默认值为 30 秒。仔细检查您的 MySql 库的文档,但对于 SqlCommand,CommandTimeout 的单位是秒而不是毫秒。
回答by Robert H
If you can show us your method, we can help find the bugs.
如果您可以向我们展示您的方法,我们可以帮助找到错误。
If history (and SO ) has taught me anything, its
如果历史(和 SO)教会了我任何东西,那么它
"You better be using Paramaterized SQL statements before posting any code"
If you are unsure on how to use parameterized commands here is an example ( taken from one of the answers where I didn't use parameterized SQL )
如果您不确定如何使用参数化命令,这里是一个示例(取自我未使用参数化 SQL 的答案之一)
var command = new MySqlCommand(
"SELECT * FROM tblPerson WHERE LastName = @Name AND Height > @Height AND BirthDate < @BirthDate", connection);
command.Parameters.AddWithValue("@Name", lastname);
command.Parameters.AddWithValue("@Height", height);
command.Parameters.AddWithValue("@Name", birthDate);
Try that if you haven't already and post some code :)
如果您还没有,请尝试并发布一些代码:)
回答by keithl8041
You can also try adding "default command timeout=360" in your connection string; eg
您也可以尝试在连接字符串中添加“默认命令超时= 360”;例如
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;default command timeout=20;
This option is available from Connector/NET version 5.1.4.
此选项可从 Connector/NET 版本 5.1.4 获得。
[I lifted this from connectionstrings.com/mysql/
[我从connectionstrings.com/mysql/ 中提取了这个

