次のコードで MySql CommandTimeout Exception をシミュレートしようとしています。CommandTimeout が 3 秒間あり、クエリの実行に約 30 秒かかります。このコードが機能しないのはなぜですか? 私は何か間違っていますか?
接続文字列に commandtimeout はありません。
connectionString="server=localhost; logging=true;user id=*****;pwd=****database=shopdb;port=3306;persist security info=true;allow user variables=false;allow zero datetime=真実"
助けてください。
using (MySqlCommand cmd = new MySqlCommand("select * from order_line", new MySqlConnection("myConnectionString"))) {
cmd.CommandTimeout = 3; // デフォルトは 30 秒
試す {
DateTime 開始 = DateTime.Now;
cmd.Connection.Open();
using (MySqlDataReader リーダー = cmd.ExecuteReader()){
while (reader.Read()){
}
DBFactory.CloseReader(リーダー);
}
cmd.Connection.Close();
DateTime end = DateTime.Now;
TimeSpan ts = 終了 - 開始;
Response.Write(ts.Seconds + "." + ts.Milliseconds);
} catch (例外例) {
Response.Write(ex.Message);
} 最後に {
DBFactory.CloseConnection(cmd);
}
}