public static Limousine AddLimousine(string maker, string model, int bouwjaar, string kenteken, float kilometerTelling, bool minibar) { MySqlConnection conn = GetConnection(); if (conn.State == ConnectionState.Open) { //Add auto to main db and return its ID int ID = AddNewAuto(maker, model, bouwjaar, kenteken, kilometerTelling); //Insert data into Limousines table string query = "INSERT INTO Limousines VALUES(" + ID + ", " + minibar + ")"; MySqlCommand cmd = new MySqlCommand(query, conn); cmd.ExecuteNonQuery(); conn.Close(); //return Limousine instance Limousine limousine = new Limousine(ID, maker, model, bouwjaar, kenteken, false, kilometerTelling, true, minibar); return(limousine); } Console.WriteLine("Failed to add new limousine to database"); return(null); }
public static Auto GetByID(int id) { MySqlConnection conn = GetConnection(); if (conn.State == ConnectionState.Open) { string query = "SELECT Maker, Model, Bouwjaar, Kenteken, MoetSchoonmaken, Kilometertelling, IsTeHuur, AutoType FROM Autos WHERE ID = " + id; MySqlCommand cmd = new MySqlCommand(query, conn); MySqlDataReader dataReader = cmd.ExecuteReader(); //Check if not empty if (!dataReader.HasRows) { Console.WriteLine("Geen Auto met dat ID in het systeem"); return(null); } //cache data from dataReader and close it because you cant have 2 active at once object[] dataCache = new object[8]; dataReader.Read(); dataReader.GetValues(dataCache); dataReader.Close(); MySqlDataReader dataReaderInherited; Auto toReturn; //switch between AutoTypes (Truck 0 and Limousine 1), make new sql query to get their unique properties from their tables in DB, create instance and return it. switch (dataCache[7]) { case 0: query = "SELECT Sleeptouw FROM Trucks where ID = " + id; cmd = new MySqlCommand(query, conn); dataReaderInherited = cmd.ExecuteReader(); dataReaderInherited.Read(); toReturn = new Truck(id, (string)dataCache[0], (string)dataCache[1], (int)dataCache[2], (string)dataCache[3], (bool)dataCache[4], (float)dataCache[5], (bool)dataCache[6], (bool)dataReaderInherited["Sleeptouw"]); break; case 1: query = "SELECT Minibar FROM Limousines WHERE ID = " + id; cmd = new MySqlCommand(query, conn); dataReaderInherited = cmd.ExecuteReader(); dataReaderInherited.Read(); toReturn = new Limousine(id, (string)dataCache[0], (string)dataCache[1], (int)dataCache[2], (string)dataCache[3], (bool)dataCache[4], (float)dataCache[5], (bool)dataCache[6], (bool)dataReaderInherited["Minibar"]); break; default: Console.WriteLine("Onbekend auto type"); return(null); } conn.Close(); return(toReturn); } return(null); }