我正在尝试通过以下代码模拟 MySql CommandTimeout 异常。我有一个 3 秒的 CommandTimeout,我的查询大约需要 30 秒才能执行。为什么这段代码不起作用?我有什么问题吗?
我的连接字符串中没有命令超时。
connectionString="server=localhost;logging=true;user id=*****;pwd=****database=shopdb;port=3306;persist security info=true;允许用户变量=false;允许零日期时间=真的”
请帮忙。
使用 (MySqlCommand cmd = new MySqlCommand("select * from order_line", new MySqlConnection("myConnectionString"))) {
cmd.CommandTimeout = 3; // 默认 30 秒
尝试 {
日期时间开始 = 日期时间。现在;
cmd.Connection.Open();
使用 (MySqlDataReader reader = cmd.ExecuteReader()){
while (reader.Read()){
}
DBFactory.CloseReader(阅读器);
}
cmd.Connection.Close();
日期时间结束 = 日期时间。现在;
时间跨度 ts = 结束 - 开始;
Response.Write(ts.Seconds + "." + ts.Milliseconds);
} 捕捉(异常前){
Response.Write(ex.Message);
} 最后 {
DBFactory.CloseConnection(cmd);
}
}