我想通過下面的代碼來模擬MySql CommandTimeout異常。 我有一個CommandTimeout 3秒,我的查詢大約需要30秒來執行。 爲什麼此代碼無法正常工作?我有什麼問題嗎?MySql CommandTimeout
在我的連接字符串中沒有commandtimeout。
connectionString="server=localhost; logging=true;user id=*****;pwd=****database=shopdb;port=3306;persist security info=true;allow user variables=false;allow zero datetime=true"
請幫忙。
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); } }
你的連接字符串究竟是什麼? – user1455836
connectionString =「server = localhost; logging = true; user id = *****; pwd = **** database = shopdb; port = 3306; persist security info = true; allow user variables = false; allow zero datetime = true「 – user2783635
我建議你仔細閱讀這個:http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html#connector-net-examples-mysqlcommand-commandtimeout – Stephan