public static ClasseSondage RecupererSondageEnBDD(int id) { using (SqlConnection connection = new SqlConnection(cheminBase)) { connection.Open(); SqlCommand sondage = new SqlCommand("SELECT* FROM Sondage WHERE IDSondage=@ID", connection); sondage.Parameters.AddWithValue("@ID", id); SqlDataReader datareader = sondage.ExecuteReader(); datareader.Read(); int idSondage = datareader.GetInt32(0); string question = datareader.GetString(1); bool choix = datareader.GetBoolean(2); string ClefSupression = datareader.GetString(3).Trim(); int NombreDeVotant = datareader.GetInt32(4); bool EtatDuSondage = datareader.GetBoolean(5); connection.Close(); sondage.Parameters.Clear(); List <ReponseSondage> Reponses = RecupereListeDereponses(id); ClasseSondage SondageCourant = new ClasseSondage(idSondage, question, Reponses, ClefSupression, choix, NombreDeVotant, EtatDuSondage); return(SondageCourant); } }
public static int CreerUnSondage(ClasseSondage Sondage) { int FKIDsondage = InsererLapremierePartieDuSondage(Sondage); InsertionReponses(Sondage, FKIDsondage); InsertionLiens(FKIDsondage); return(FKIDsondage); }
public static int InsererLapremierePartieDuSondage(ClasseSondage Sondage) { using (SqlConnection connection = new SqlConnection(cheminBase)) { connection.Open(); SqlCommand TableSondage = new SqlCommand("INSERT INTO Sondage(Question,ChoixMultiple,ClefDeSupression,NombreDeVotants,EtatDuSondage) OUTPUT INserted.IDSondage VALUES (@Question,@ChoixMultiple,@ClefDeSupression,@NombreDeVotant,@EtatDuSondage)", connection); TableSondage.Parameters.AddWithValue("@Question", Sondage.QuestionDuSondage); TableSondage.Parameters.AddWithValue("@ChoixMultiple", Sondage.QuestionChoixMultiples); TableSondage.Parameters.AddWithValue("@NombreDeVotant", Sondage.NombreDeVotant); TableSondage.Parameters.AddWithValue("@EtatDuSondage", Sondage.EtatDuSondage); TableSondage.Parameters.AddWithValue("@ClefDeSupression", Sondage.ClefDeSupression); int IdDuSondageInsere = (int)TableSondage.ExecuteScalar(); return(IdDuSondageInsere); } }
public static void InsertionReponses(ClasseSondage sondage, int fkid) { foreach (ReponseSondage reponse in sondage.ListeDeReponse) { //const string cheminBase = @"Server=.\sqlexpress;Initial Catalog = SONDAGEGOLD; Integrated Security = True"; using (SqlConnection connection = new SqlConnection(cheminBase)) { connection.Open(); SqlCommand TableReponse = new SqlCommand("INSERT INTO Reponse (FKIDSondage,Reponses,NombreDeVote) VALUES (@FKIDSondage,@Reponses,@NombreDeVote)", connection); TableReponse.Parameters.AddWithValue("@FKIDSondage", fkid); TableReponse.Parameters.AddWithValue("@Reponses", reponse.Reponse); TableReponse.Parameters.AddWithValue("@NombreDeVote", reponse.NombreDeVote); TableReponse.ExecuteReader(); connection.Close(); TableReponse.Parameters.Clear(); } } }