I'm trying to simulate a MySql CommandTimeout Exception by the following code. I have a CommandTimeout for 3 seconds, and my query takes about 30 seconds to execute. Why does this code not working? Do I something wrong?
There is no commandtimeout in my connectionstring.
connectionString="server=localhost; logging=true;user id=*****;pwd=****database=shopdb;port=3306;persist security info=true;allow user variables=false;allow zero datetime=true"
Please help.
using (MySqlCommand cmd = new MySqlCommand("select * from order_line", new MySqlConnection("myConnectionString"))) {
cmd.CommandTimeout = 3; // default 30 seconds
try {
DateTime start = DateTime.Now;
cmd.Connection.Open();
using (MySqlDataReader reader = cmd.ExecuteReader()){
while (reader.Read()){
}
DBFactory.CloseReader(reader);
}
cmd.Connection.Close();
DateTime end = DateTime.Now;
TimeSpan ts = end - start;
Response.Write(ts.Seconds + "." + ts.Milliseconds);
} catch (Exception ex) {
Response.Write(ex.Message);
} finally {
DBFactory.CloseConnection(cmd);
}
}