public void Insert_Power_year() { try { string strYesterday = DateTime.Now.AddYears(-1).ToString("yyyy-01-01 00:00:00"); string strToday = DateTime.Now.ToString("yyyy-01-01 00:00:00"); String sql = "SELECT * FROM power_data_month WHERE DATETIME BETWEEN '" + strYesterday + "' and '" + strToday + "' ORDER BY DATETIME desc"; DataSet ds = new DataSet(); MySqlDataAdapter adpt = new MySqlDataAdapter(sql, conn); adpt.Fill(ds); sPower total_power = GetPowerFromDatabase(ds); sql = "INSERT INTO power_data_year (DATETIME, PCS_CHARGE_POWER, PCS_DISCHARGE_POWER, BMS_CHARGE_POWER, BMS_DISCHARGE_POWER) " + "VALUES ('" + strYesterday + "','" + total_power.PCS_CHARGE_POWER + "','" + total_power.PCS_DISCHARGE_POWER + "','" + total_power.BMS_CHARGE_POWER + "','" + total_power.BMS_DISCHARGE_POWER + "')"; MySqlCommand cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private sPower GetPowerFromDatabase(DataSet ds) { sPower power = new sPower(); int total_count = new int(); foreach (DataTable table in ds.Tables) { foreach (DataRow dr in table.Rows) { power.PCS_CHARGE_POWER = power.PCS_CHARGE_POWER + Convert.ToDouble(dr["PCS_CHARGE_POWER"]); power.PCS_DISCHARGE_POWER = power.PCS_DISCHARGE_POWER + Convert.ToDouble(dr["PCS_DISCHARGE_POWER"]); power.BMS_CHARGE_POWER = power.BMS_CHARGE_POWER + Convert.ToDouble(dr["BMS_CHARGE_POWER"]); power.BMS_DISCHARGE_POWER = power.BMS_DISCHARGE_POWER + Convert.ToDouble(dr["BMS_DISCHARGE_POWER"]); total_count = total_count + 1; } } if (total_count == 0) { total_count = 1; // To protect zero-division Exception } power.PCS_CHARGE_POWER = power.PCS_CHARGE_POWER / ds.Tables.Count; power.PCS_DISCHARGE_POWER = power.PCS_DISCHARGE_POWER / ds.Tables.Count; power.BMS_CHARGE_POWER = power.BMS_CHARGE_POWER / ds.Tables.Count; power.BMS_DISCHARGE_POWER = power.BMS_DISCHARGE_POWER / ds.Tables.Count; return(power); }
public static sPower operator+(sPower a, sPower b) { sPower newPower = new sPower(); newPower.BMS_CHARGE_POWER = a.BMS_CHARGE_POWER + b.BMS_CHARGE_POWER; newPower.BMS_DISCHARGE_POWER = a.BMS_DISCHARGE_POWER + b.BMS_DISCHARGE_POWER; newPower.PCS_CHARGE_POWER = a.PCS_CHARGE_POWER + b.PCS_CHARGE_POWER; newPower.PCS_DISCHARGE_POWER = a.PCS_DISCHARGE_POWER + b.PCS_DISCHARGE_POWER; return(newPower); }