/// <summary> /// Add a video game in the DB and get the id of the video game inserted in /// the DB /// </summary> /// <param name="game">video game to insert into the DB</param> /// <returns>the id of the video game inserted into the DB</returns> public long AddVideoGame(VideoGame game) { long videogameId = 0; try { int idType = GetType(game.Type); //Check if connection is not closed, otherwise open it OpenConnection(); // Création d'une commande SQL en fonction de l'objet connection MySqlCommand cmd = this.connection.CreateCommand(); // Requête SQL : add data into videogame table cmd.CommandText = "INSERT INTO videogames (name, dateOutput, type_id) VALUES (@nom, @dateOutput, @fkType)"; // utilisation de l'objet contact passé en paramètre cmd.Parameters.AddWithValue("@nom", game.Name); cmd.Parameters.AddWithValue("@dateOutput", game.DateOutput); cmd.Parameters.AddWithValue("@fkType", idType); // Exécution de la commande SQL cmd.ExecuteNonQuery(); videogameId = cmd.LastInsertedId; foreach (string platform in game.ListPlatform) { int platFormId = GetPlatformId(platform); //insert data into the platformvideogame table cmd.CommandText = "INSERT INTO platformvideogames (platform_id, videogame_id) VALUES (@idplatform, @idvideogame)"; // utilisation de l'objet contact passé en paramètre cmd.Parameters.AddWithValue("@idplatform", platFormId); cmd.Parameters.AddWithValue("@idvideogame", videogameId); // Exécution de la commande SQL cmd.ExecuteNonQuery(); //on vide les paramètres sinon le programme dit qu'ils existent déjà cmd.Parameters.Clear(); } foreach (Editor ed in game.Editor) { long editorId = ed.Id; if (editorId == 0) { //insert into the editor table if the editor does not exist yet cmd.CommandText = "INSERT INTO editors (name) VALUES (@nameeditor)"; // utilisation de l'objet contact passé en paramètre cmd.Parameters.AddWithValue("@nameeditor", ed); // Exécution de la commande SQL cmd.ExecuteNonQuery(); editorId = cmd.LastInsertedId; cmd.Parameters.Clear(); } // editorvideogame table cmd.CommandText = "INSERT INTO editorvideogames (editor_id, videogame_id) VALUES (@fkeditor, @fkvideogame)"; // utilisation de l'objet contact passé en paramètre cmd.Parameters.AddWithValue("@fkeditor", editorId); cmd.Parameters.AddWithValue("@fkvideogame", videogameId); // Exécution de la commande SQL cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } foreach (Developer dev in game.Developer) { long developerId = dev.Id; if (developerId == 0) { //insert data into the developer table if it does not exist yet cmd.CommandText = "INSERT INTO developers (name) VALUES (@namedeveloper)"; // utilisation de l'objet contact passé en paramètre cmd.Parameters.AddWithValue("@namedeveloper", dev); // Exécution de la commande SQL cmd.ExecuteNonQuery(); developerId = cmd.LastInsertedId; cmd.Parameters.Clear(); } //insert into developervideogame table cmd.CommandText = "INSERT INTO developervideogames (developer_id, videogame_id) VALUES (@fkdev, @fkvideogame)"; // utilisation de l'objet contact passé en paramètre cmd.Parameters.AddWithValue("@fkdev", developerId); cmd.Parameters.AddWithValue("@fkvideogame", videogameId); // Exécution de la commande SQL cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } } catch (Exception ex) { // Gestion des erreurs : // Possibilité de créer un Logger pour les exceptions SQL reçus throw new VgSQLException("Erreur lors de l'insertion d'un jeu vidéo : " + ex.Message); } return(videogameId); }