using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using CG67.FicheCollege.Domaine; using CG67.FicheCollege.Interface; namespace CG67.FicheCollege.Entrepot { public class EntrepotChiffresSignificatifs : EntrepotBase, IEntrepotChiffresSignificatifs { public ChiffresSignificatifs GetByAnnee(int annee) { ChiffresSignificatifs resultat = new ChiffresSignificatifs(); string sum; using (SqlConnection connexion = new SqlConnection(this.ChaineDeConnexion)) { try { connexion.Open(); using (SqlCommand command = connexion.CreateCommand()) { //Il n'est pas possible de tout faire en une requete ... //Du coup, une requete a été crée par chiffre (deux dans le cas du montant total des financements. command.CommandText = "SELECT sum(Dotation.Montant) as totalDotation FROM Dotation WHERE Dotation.Annee = @Annee"; command.Parameters.AddWithValue("@Annee", annee); sum = command.ExecuteScalar().ToString(); resultat.MontantTotalFinancements = string.IsNullOrEmpty(sum) ? 0 : Convert.ToDouble(sum); //using (SqlDataReader dr = command.ExecuteReader()) //{ // while (dr.Read()) // { // resultat.MontantTotalFinancements += Convert.ToDouble(dr["totalDotation"].ToString()); // } //} command.CommandText = "SELECT sum(MontantSubvention) as totalSubvention FROM Etablissement_ActionEducative WHERE Etablissement_ActionEducative.Annee = @Annee"; sum = command.ExecuteScalar().ToString(); resultat.MontantTotalFinancements = string.IsNullOrEmpty(sum) ? 0 : System.Convert.ToDouble(sum); //using (SqlDataReader dr2 = command.ExecuteReader()) //{ // while (dr2.Read()) // { // resultat.MontantTotalFinancements += Convert.ToDouble(dr2["totalSubvention"].ToString()); // } //} //command.CommandText = "SELECT sum(TotalEleves) as totalEleves FROM Effectif WHERE Effectif.Annee = @Annee"; //command.CommandText ="SELECT Sum(EffectifTotal.EffectifTotal) AS SommeDeEffectifTotal FROM EffectifTotal GROUP BY EffectifTotal.Annee HAVING (((EffectifTotal.Annee)=@Annee) AND (Typedonnees='R'))"; //command.CommandText = "SELECT Sum(EffectifTotal.EffectifTotal) AS SommeDeEffectifTotal FROM EffectifTotal WHERE Annee = @Annee "; command.CommandText = "SELECT Sum(Effectif.TotalEleves) AS SommeDeEffectifTotal FROM Effectif GROUP BY Effectif.Annee HAVING (Effectif.Annee = @Annee) "; //command.Parameters.AddWithValue("@Annee", annee); sum = command.ExecuteScalar().ToString(); resultat.NbTotalEleves = string.IsNullOrEmpty(sum) ? (Int32) 0 : Convert.ToInt32 (sum); command.CommandText = "SELECT sum(Montant) as totalInvest FROM Investissement WHERE Investissement.Annee = @Annee"; sum = command.ExecuteScalar().ToString(); resultat.MontantTotalInvestissement = string.IsNullOrEmpty(sum) ? 0 : Convert.ToDouble (sum); //using (SqlDataReader dr4 = command.ExecuteReader()) //{ // while (dr4.Read()) // { // resultat.MontantTotalInvestissement += Convert.ToDouble(dr4["totalInvest"].ToString()); // } //} // command.CommandText = "SELECT CoutTotal as totalTransport FROM Transport WHERE Transport.Annee = @Annee"; // using (SqlDataReader dr5 = command.ExecuteReader()) // { // while (dr5.Read()) // { // resultat.MontantTotalTransportScolaire += Convert.ToDouble(dr5["totalTransport"].ToString()); // } // } } } catch (Exception erreurInterne) { throw new Exception(erreurInterne.ToString()); } finally { if (connexion.State == ConnectionState.Open) connexion.Close(); } } return resultat; } } }