C# MySqlException:Timeout expired-增加连接超时没有任何效果

C# MySqlException:Timeout expired-增加连接超时没有任何效果,c#,mysql,connection-timeout,C#,Mysql,Connection Timeout,我有一个查询,随着数据库大小的增加,执行该查询的时间会更长。查询是经过优化和必要的,但我的C控制台应用程序最近给了我以下错误: Unhandled Exception: MySql.Data.MySqlClient.MySqlException: Timeout expired. var command = new MySqlCommand( "SELECT * FROM tblPerson WHERE LastName = @Name AND Height > @Height

我有一个查询,随着数据库大小的增加,执行该查询的时间会更长。查询是经过优化和必要的,但我的C控制台应用程序最近给了我以下错误:

Unhandled Exception: MySql.Data.MySqlClient.MySqlException: Timeout expired.
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);
增加连接字符串中的连接超时没有帮助;我从

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);
连接超时=28800

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);

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);
连接超时=128800

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);
但是,尽管有了这个改变,我仍然会犯错误

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);
如果从MySQL workbench运行查询,只需10秒左右,因此我不确定如何防止此未处理的异常

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);

除了查询所需的时间之外,还有其他因素会导致此异常吗?

如果您能向我们展示您的方法,我们可以帮助您找到错误

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);
如果说历史和其他东西教会了我什么,那就是

"You better be using Paramaterized SQL statements before posting any code" 
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);
如果您不确定如何使用参数化命令,这里有一个示例,摘自我没有使用参数化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);

如果您还没有发布一些代码,请尝试这样做:

我以前遇到过这个问题。ConnectTimeout属性仅适用于连接到数据库时发生的超时,而不适用于查询

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);

CommandTimeout指定查询返回的等待时间。我相信默认值是30秒。仔细检查MySql库的文档,但是对于SqlCommand,CommandTimeout的单位是秒而不是毫秒。

您也可以尝试在连接字符串中添加默认的CommandTimeout=360;乙二醇

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);
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;default command timeout=20;
此选项可从Connector/NET 5.1.4版获得

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);

[我从

中取消了此操作,更改了CommandTimeout而不是ConnectionTimeout。未处理的异常:System.ArgumentException:关键字不受支持。参数名称:Application.MainClass.MainString处的connecttimeout[]args噢,我需要更仔细地阅读您的命令超时。不是.public静态字符串ConnectionString=server=192.168.1.2;user=user1;database=database1;port=3306;password=testPW;Pooling=true;Connect Timeout=28800;Command Timeout=28800;我仍在测试中。谢谢Tim!自从我设置了命令超时之后,我还没有经历过超时,就像s一样hown在我上面的连接字符串中。在MySQL中也是秒。另外-0是无超时。
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);