EntrepotChiffresSignificatifs.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using CG67.FicheCollege.Domaine;
  7. using CG67.FicheCollege.Interface;
  8. namespace CG67.FicheCollege.Entrepot
  9. {
  10. public class EntrepotChiffresSignificatifs : EntrepotBase, IEntrepotChiffresSignificatifs
  11. {
  12. public ChiffresSignificatifs GetByAnnee(int annee)
  13. {
  14. ChiffresSignificatifs resultat = new ChiffresSignificatifs();
  15. using (SqlConnection connexion = new SqlConnection(this.ChaineDeConnexion))
  16. {
  17. try
  18. {
  19. connexion.Open();
  20. using (SqlCommand command = connexion.CreateCommand())
  21. {
  22. //Il n'est pas possible de tout faire en une requete ...
  23. //Du coup, une requete a été crée par chiffre (deux dans le cas du montant total des financements.
  24. command.CommandText = "SELECT sum(Dotation.Montant) as totalDotation FROM Dotation WHERE Dotation.Annee = @Annee";
  25. command.Parameters.AddWithValue("@Annee", annee);
  26. using (SqlDataReader dr = command.ExecuteReader())
  27. {
  28. while (dr.Read())
  29. {
  30. resultat.MontantTotalFinancements += Convert.ToDouble(dr["totalDotation"].ToString());
  31. }
  32. }
  33. command.CommandText = "SELECT sum(MontantSubvention) as totalSubvention FROM Etablissement_ActionEducative WHERE Etablissement_ActionEducative.Annee = @Annee";
  34. using (SqlDataReader dr2 = command.ExecuteReader())
  35. {
  36. while (dr2.Read())
  37. {
  38. resultat.MontantTotalFinancements += Convert.ToDouble(dr2["totalSubvention"].ToString());
  39. }
  40. }
  41. command.CommandText = "SELECT sum(TotalEleves) as totalEleves FROM Effectif WHERE Effectif.Annee = @Annee";
  42. using (SqlDataReader dr3 = command.ExecuteReader())
  43. {
  44. while (dr3.Read())
  45. {
  46. resultat.NbTotalEleves += Convert.ToInt16(dr3["totalEleves"].ToString());
  47. }
  48. }
  49. command.CommandText = "SELECT sum(Montant) as totalInvest FROM Investissement WHERE Investissement.Annee = @Annee";
  50. using (SqlDataReader dr4 = command.ExecuteReader())
  51. {
  52. while (dr4.Read())
  53. {
  54. resultat.MontantTotalInvestissement += Convert.ToDouble(dr4["totalInvest"].ToString());
  55. }
  56. }
  57. command.CommandText = "SELECT CoutTotal as totalTransport FROM Transport WHERE Transport.Annee = @Annee";
  58. using (SqlDataReader dr5 = command.ExecuteReader())
  59. {
  60. while (dr5.Read())
  61. {
  62. resultat.MontantTotalTransportScolaire += Convert.ToDouble(dr5["totalTransport"].ToString());
  63. }
  64. }
  65. }
  66. }
  67. catch (Exception erreurInterne)
  68. {
  69. throw new Exception(erreurInterne.ToString());
  70. }
  71. finally
  72. {
  73. if (connexion.State == ConnectionState.Open)
  74. connexion.Close();
  75. }
  76. }
  77. return resultat;
  78. }
  79. }
  80. }