예제 #1
0
        //REQUETE QUI RECUPERE UN SONDAGE
        #region [BDD] - Recupère un sondage existant
        /// <summary>
        /// Récupère un sondage existant,
        /// si l'ID n'existe pas en BDD retourne une Exception.
        /// Sinon ajoute dans une liste les sondages trouvés.
        /// </summary>
        public static Sondage RecupereSondageEnBDD(int Id)
        {
            List <Choix> ReponsesCourante = new List <Choix>();
            string       Question         = string.Empty;
            bool         ChoixMultiple;
            int          NbVotes;

            using (SqlConnection connection = new SqlConnection(ChaineConnexionBDD))
            {
                connection.Open();

                //Récupère la question et le type de choix
                SqlCommand command = new SqlCommand("SELECT Question, ChoixMultiple, NbVotes FROM Sondage WHERE IdSondage =@id", connection);
                command.Parameters.AddWithValue("@id", Id);
                using (SqlDataReader DataReader = command.ExecuteReader())
                {
                    if (DataReader.Read())
                    {
                        Question      = (string)DataReader["Question"];
                        ChoixMultiple = (bool)DataReader["ChoixMultiple"];
                        NbVotes       = (int)DataReader["NbVotes"];
                    }
                    else
                    {
                        throw new SondageNonTrouveException();
                    }
                }

                //Prend les réponses trouvées et les insère dans la liste ReponseCourante
                SqlCommand command2 = new SqlCommand("SELECT IdChoix, IntituleChoix FROM Choix WHERE FK_Id_Sondage =@id", connection);
                command2.Parameters.AddWithValue("@id", Id);
                using (SqlDataReader DataReader2 = command2.ExecuteReader())
                {
                    while (DataReader2.Read())
                    {
                        int    IdChoix     = (int)DataReader2["IdChoix"];
                        string valeurChoix = (string)DataReader2["IntituleChoix"];

                        Choix Reponse = new Choix(IdChoix, valeurChoix);

                        ReponsesCourante.Add(Reponse);
                    }
                }
            }

            Sondage RecupSonsage = new Sondage(Id, Question, ReponsesCourante, ChoixMultiple, NbVotes);

            return(RecupSonsage);
        }
예제 #2
0
        //RECHERCHE LES REPONSES ET NOMBRE DE VOTES PAR REPONSE
        #region [BDD] - Ajoute et tri les réponse du sondage par ordre alphabétique
        /// <summary>
        /// Récupère la question, si c'est un choix multiple ou non et le nombre de votants du sondage.
        /// Puis cherche les réponses et l'id de la réponse en les triant par ordre décroissant et en les insérant dans une liste.
        /// Puis renvoie le sondage.
        /// </summary>
        public static Sondage RecupereReponseEtNombreVoteBDD(int Id)
        {
            List <Choix> ReponsesCourante = new List <Choix>();
            string       Question         = string.Empty;
            bool         ChoixMultiple;
            int          NbVotes;

            using (SqlConnection connection = new SqlConnection(ChaineConnexionBDD))
            {
                connection.Open();

                //Récupère la question et le type de choix et le nombre de votes.
                SqlCommand command = new SqlCommand("SELECT Question, ChoixMultiple, Nbvotes FROM Sondage WHERE IdSondage =@id", connection);
                command.Parameters.AddWithValue("@id", Id);
                using (SqlDataReader DataReader = command.ExecuteReader())
                {
                    if (DataReader.Read())
                    {
                        Question      = (string)DataReader["Question"];
                        ChoixMultiple = (bool)DataReader["ChoixMultiple"];
                        NbVotes       = (int)DataReader["NbVotes"];
                    }
                    else
                    {
                        throw new SondageNonTrouveException();
                    }
                }

                //Récupère l'ID, le nom et le nombre de votes de la réponse en les insèrant dans une liste et en les classant par ordre décroissant.
                SqlCommand command2 = new SqlCommand("SELECT IdChoix, IntituleChoix, NbVotes FROM Choix WHERE FK_Id_Sondage =@id ORDER BY NbVotes DESC", connection);
                command2.Parameters.AddWithValue("@id", Id);
                using (SqlDataReader DataReader2 = command2.ExecuteReader())
                {
                    while (DataReader2.Read())
                    {
                        int    IdChoix     = (int)DataReader2["IdChoix"];
                        string valeurChoix = (string)DataReader2["IntituleChoix"];
                        int    NombreVotes = (int)DataReader2["NbVotes"];

                        Choix Reponse = new Choix(IdChoix, valeurChoix, NombreVotes);

                        ReponsesCourante.Add(Reponse);
                    }
                }
            }
            Sondage RecupSonsage = new Sondage(Id, Question, ReponsesCourante, ChoixMultiple, NbVotes);

            return(RecupSonsage);
        }