public void writeAccumulEnergy(Mercury230.accumulEnergy energystruct, int tarif, Mercury230.peroidQuery period, Mercury230_DatabaseSignals device, byte month) { DateTime datetime = new DateTime(); if (month == 0) { datetime = DateTime.Now; } else { datetime = (month > DateTime.Now.Month) ? new DateTime(DateTime.Now.AddYears(-1).Year, month, 1, 0, 0, 0) : new DateTime(DateTime.Now.Year, month, 1, 0, 0, 0); } string CommandText = "INSERT INTO meter230 ( `addr`, `energy_active_in`, `energy_reactive_in`, `energy_reactive_out`, `oleDT` , `tariff`, `period` , `id`, `month`) VALUES (" + device.address.ToString() + ", " + energystruct.active_energy_in + ", " + energystruct.reactive_energy_in + ", " + energystruct.reactive_energy_out + ", " + datetime.ToOADate() + ", " + tarif.ToString() + ", '" + Convert.ToString((int)period) + "', " + device.serial_number.ToString() + " , " + month + " )"; SendData(CommandText); }
public DataTable GiveSQLrecordingPeriod(Mercury230_DatabaseSignals device, string table, Mercury230.peroidQuery period, byte month, byte tariff) { ConnectionState firstSTate = ConnectionState.Open; DateTime dt = DateTime.Now; try { if (myConnec.State != ConnectionState.Open) { myConnec.Open(); firstSTate = ConnectionState.Closed; } DateTime dt_beginday = new DateTime(); DateTime dt_ending = new DateTime(); if (month == 0) { dt_beginday = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0); dt = DateTime.Now.AddDays(1); dt_ending = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0); } else { dt_beginday = (month > dt.Month) ? new DateTime(dt.AddYears(-1).Year, month, 1, 0, 0, 0) : new DateTime(dt.Year, month, 1, 0, 0, 0); dt_ending = (month > dt.Month) ? new DateTime(dt.Year, 1, 1, 0, 0, 0) : new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0); } string CommandText = "SELECT * FROM " + table + " WHERE `addr` =" + device.address.ToString() + " AND `oleDT` >= " + dt_beginday.ToOADate() + " AND `oleDT` <= " + dt_ending.ToOADate() + " AND `period` = " + (int)period + " AND `month` = " + month + " AND `tariff` = " + tariff; MySqlCommand myCommand = new MySqlCommand(CommandText, myConnec); DataTable dtable = new DataTable(); MySqlDataReader dr = myCommand.ExecuteReader(); dtable.Load(dr); if (firstSTate == ConnectionState.Closed) { myConnec.Close(); } return(dtable); } catch (Exception e) { Console.WriteLine("Ошибка БД {0}", e.Message); return(null); } }