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(); 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); 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"; 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"; using (SqlDataReader dr3 = command.ExecuteReader()) { while (dr3.Read()) { resultat.NbTotalEleves += Convert.ToInt16(dr3["totalEleves"].ToString()); } } command.CommandText = "SELECT sum(Montant) as totalInvest FROM Investissement WHERE Investissement.Annee = @Annee"; 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; } } }