public void PutInDataBase(List <Sensors> theObjToPush, DateTime endDate) { string dbConnectionString = string.Format("server={0};uid={1};pwd={2};database={3};", serverIp, username, password, databaseName); MySqlCommand cmd = new MySqlCommand(); MySqlConnection conn = new MySqlConnection(dbConnectionString); cmd.Connection = conn; conn.Open(); TimerToUse time = new TimerToUse(); foreach (var item in theObjToPush) { double result = 0; if (item.type == true) { result = Math.Round((item.value / item.numberOfValue), 0); } else { result = item.value / item.numberOfValue; } cmd.CommandText = "INSERT INTO stats(sensor_id,value,stat_type_id,date) VALUES(" + item.sensor_id + "," + result + ",1,'" + endDate.ToString("yyyy-MM-dd HH:mm:ss") + "');"; Console.WriteLine(cmd.CommandText); cmd.ExecuteNonQuery(); } conn.Close(); }
public List <Sensors> GetFromDataBase(DateTime startDate, DateTime endeDate) { TimerToUse time = new TimerToUse(); string dbConnectionString = string.Format("server={0};uid={1};pwd={2};database={3};", serverIp, username, password, databaseName); string query = "SELECT * FROM datas WHERE date BETWEEN '" + startDate.ToString("yyyy-MM-dd HH:mm:ss") + "' AND '" + endeDate.ToString("yyyy-MM-dd HH:mm:ss") + "';"; Console.WriteLine(query); var conn = new MySqlConnection(dbConnectionString); conn.Open(); var cmd = new MySqlCommand(query, conn); var reader = cmd.ExecuteReader(); List <Sensors> theListOfSensor = new List <Sensors>(); int i = 0; while (reader.Read()) { var someValue = reader["id"]; var someValue1 = reader["sensor_id"]; // sensor var someValue2 = reader["value"]; // valeur var someValue4 = reader["date"]; // valeur var someValue3 = reader["isBool"]; // type if (someValue3 == null) { someValue3 = false; } else { someValue3 = true; } i++; var sensorValue = new Sensors(); sensorValue.sensor_id = someValue1; sensorValue.value = someValue2; sensorValue.type = someValue3; sensorValue.numberOfValue = 1; theListOfSensor.Add(sensorValue); } conn.Close(); Console.WriteLine("ID number is : " + i); return(theListOfSensor); }