using MySql.Data.MySqlClient; using PaymentLogs.Domain; using PaymentLogs.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Security.Policy; using System.Text; using System.Threading.Tasks; namespace PaymentLogs.Repositories { internal class StudentRepo : Repo { public override void Load() { DataBase db = new DataBase(); db.openConnection(); _data.Clear(); string sql = " SELECT * FROM students "; MySqlCommand cmd = new MySqlCommand(sql, db.getConnection()); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Student student = new Student() { Id = reader.GetInt32("id"), FCs = reader.GetString("fcs"), dateOfBirth = DateTime.ParseExact(reader.GetString("date_of_birth"), "yyyy-MM-dd",null), Address = reader.GetString("address"), CourseId = reader.GetInt32("course_id"), Paid = reader.GetInt32("paid") }; _data[student.Id] = student; } db.closeConnection(); } public override void Save() { DataBase db = new DataBase(); db.openConnection(); foreach (Student student in All()) { string sql = $" INSERT INTO `students` (`id`, `fcs`, `date_of_birth`, `address`, `course_id`, `paid`) " + $"VALUES ('{student.Id}','{student.FCs}','{ String.Format("{0:yyyy-MM-dd}", student.dateOfBirth) }', '{student.Address}'," + $"'{student.CourseId}', '{student.Paid}') " + $"ON DUPLICATE KEY UPDATE `fcs`='{student.FCs}', `date_of_birth`='{String.Format("{0:yyyy-MM-dd}", student.dateOfBirth)}'," + $"`address`='{student.Address}', `course_id`='{student.CourseId}', `paid`='{student.Paid}'"; MySqlCommand cmd = new MySqlCommand(sql, db.getConnection()); cmd.ExecuteNonQuery(); } db.closeConnection(); } } }