//手動設定需量反應事件 private void button2_Click(object sender, EventArgs e) { string currentTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");// 2018-04-17 01:20:26 string start_at = dtp.Text + " " + dtpTime.Text + ":00";; string end_at = dtp2.Text + " " + dtpTime2.Text + ":00";; DateJob.getTimeDiff(start_at, end_at); //取得時間差 double Start_Now = DateJob.getStart_Now(); //開始 減 現在 double End_Start = DateJob.getEnd_Start(); //結束 減 開始 if (Start_Now > 0 && End_Start > 0) { //判斷 需量時間合理 (現在-開始 >0 && 結束-開始>0 dbName = "hems"; dtName = "dr"; drJob.dr_IfRepeat_Insert(dbName, dtName, start_at, end_at, 0); dbName = null; dtName = null; label13.Visible = true; label13.Text = "需量反應事件 \n 設定完成"; drdone = "done"; } else if (Start_Now < 0) { MessageBox.Show("請設定開始時間於未來"); } else if (Start_Now > 0 && End_Start < 0) { MessageBox.Show("請設定結束時間大於開始時間"); } //label13.Text = "Start_Now : " + Start_Now + "\n" +"End_Start : " + End_Start + " \n"; }
//--------------------------------------------------------------------------------------------------- ////(自動手動)新增dr事件(每小時) 並判斷是否有重複 auto = 1 自動 / auto = 0 手動 public static void dr_IfRepeat_Insert(string dbName, string dtName, string startTime, string endTime, int auto) { //currentTimeStr = tomorrow string currentTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // now string current1TimeStr = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"); //tomorrow DateTime currentTime = System.DateTime.Now; //now string YMD = ""; if (auto == 1) { string[] tomorrow = current1TimeStr.Split(' '); //tomorrow年月日 YMD = tomorrow[0]; } else if (auto == 0) { // string[] NonAuto = startTime.Split(' '); //startTime年月日 //YMD = NonAuto[0]; } DateJob.getTimeDiff(startTime, endTime); int duration = (int)DateJob.getEnd_Start(); //duration //建立 DataSet DataTable dt = new DataTable(); int datarow_num; string Qrequest = "SELECT COUNT(*) FROM " + dtName + " WHERE start_at LIKE " + "'" + YMD + "%" + "'"; datarow_num = DataRowNumber(dbName, Qrequest); //Allow Zero Datetime = true 讓Datetime可為 0000-00-00 00:00:00 string config = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=" + dbName + "; Allow Zero Datetime = true"; MySqlConnection connection = new MySqlConnection(config); try { //使用 MySqlDataAdapter 查詢資料,並將結果存回 DataSet 當做名為 test1 的 DataTable string query = "SELECT * FROM dr WHERE start_at LIKE " + "'" + YMD + "%" + "'"; MySqlDataAdapter dataAdapter1 = new MySqlDataAdapter(query, connection); //MySqlDataAdapter自動 open &close dataAdapter1.Fill(dt); string[] Lstart_at = new string[datarow_num]; int checkDRinsert = 0; for (int i = 0; i < datarow_num; i++) //檢查資料庫是否已有dr事件 避免重複 { Lstart_at[i] = Convert.ToDateTime(dt.Rows[i]["start_at"]).ToString("yyyy-MM-dd HH:mm:ss"); if (Lstart_at[i].Equals(startTime)) { checkDRinsert = checkDRinsert + 1; } //已新增過資料 else { } //新增Dr事件 } // MessageBox.Show(Lstart_at[0] + "\n" + dt.Rows[0]["start_at"]); //insert(dr事件時間); if (checkDRinsert == 0) { dr_set(startTime, endTime, duration, currentTimeStr, dbName); } datarow_num = 0; checkDRinsert = 0; } catch (MySqlConversionException ee) { MessageBox.Show("MySqlConversionException : \n" + ee); } catch (IndexOutOfRangeException rangeE) { MessageBox.Show("IndexOutOfRangeException : " + rangeE); } catch (FormatException Fe) { MessageBox.Show("FormatException : " + Fe); } //開始 結束 現在 持續 }