简体   繁体   English

MySqlException:超时已过期 - 增加连接超时没有效果

[英]MySqlException: Timeout expired - Increasing Connection Timeout Has Had No Effect

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? 除了“查询所花费的时间”之外还有其他什么可以产生这种异常吗?

I've had this problem before. 我以前遇到过这个问题。 ConnectTimeout property only applies to time outs that occur when connecting to the database, not for queries. ConnectTimeout属性仅适用于连接到数据库时发生的超时,而不适用于查询。

CommandTimeout however specifies how long it should wait for the query to return. 但是, CommandTimeout指定等待查询返回的时间。 I believe the default is 30 seconds. 我相信默认值是30秒。 Double check the documentation for your MySql library, but for SqlCommand the CommandTimeout is in Seconds not milliseconds. 仔细检查MySql库的文档,但对于SqlCommand,CommandTimeout的秒数不是毫秒。

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 :) 如果你还没有尝试,并发布一些代码:)

You can also try adding "default command timeout=360" in your connection string; 您还可以尝试在连接字符串中添加“default command timeout = 360”; eg 例如

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/解除了这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM