EntrepotLogement.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 EntrepotLogement : EntrepotBase, IEntrepotLogement
  11. {
  12. public Logement GetByCodeRNE(string codeRNE)
  13. {
  14. Logement resultat = new Logement();
  15. using (SqlConnection connexion = new SqlConnection(this.ChaineDeConnexion))
  16. {
  17. try
  18. {
  19. connexion.Open();
  20. using (SqlCommand command = connexion.CreateCommand())
  21. {
  22. command.CommandText = "SELECT count(Logement.Id) as NbrLogements, Contact.Fonction FROM Logement LEFT JOIN Logement_Contact ON (Logement.Id = Logement_Contact.IdLogement) LEFT JOIN Contact ON (Logement_Contact.IdContact = Contact.Id) WHERE Logement.CodeRNE = @RNE AND Logement_Contact.DateSortie is NULL AND Contact.CodeRNE = @RNE Group BY Contact.Fonction";
  23. command.Parameters.AddWithValue("@RNE", codeRNE);
  24. using (SqlDataReader dr = command.ExecuteReader())
  25. {
  26. while (dr.Read())
  27. {
  28. resultat.NbrLogements += Convert.ToInt16(dr["NbrLogements"].ToString());
  29. // Si la fonction est de type ATC* (maintenance, entretien, etc...) on ajoute le nombre de logements au nombre d'agents logés (cf. requete)
  30. if ((String.Compare(dr["Fonction"].ToString().Substring(0, 3), "ATC")) == 0)
  31. resultat.NbrAgentsLoges += Convert.ToInt16(dr["NbrLogements"].ToString());
  32. }
  33. }
  34. }
  35. }
  36. catch (Exception erreurInterne)
  37. {
  38. throw new Exception(" " + erreurInterne);
  39. }
  40. finally
  41. {
  42. if (connexion.State == ConnectionState.Open)
  43. connexion.Close();
  44. }
  45. }
  46. return resultat;
  47. }
  48. }
  49. }