Beispiel #1
0
        private static void GetPersonnesFromDataReader(List <Personne> listPersonnes, SqlDataReader reader)
        {
            if (listPersonnes.Count == 0 || (int)reader["EmployeeID"] != listPersonnes.Last().EmployeeId)
            {
                var pers = new Personne();

                pers.EmployeeId = (int)reader["EmployeeID"];  // ou bien (int)reader["EmployeeID"];
                pers.Nom        = (string)reader["LastName"]; // ...
                pers.Prenom     = (string)reader["FirstName"];
                pers.NomPrenom  = pers.Nom + " " + pers.Prenom;


                if (reader["Photo"] != DBNull.Value)
                {
                    pers.Photo = ConvertBytesToImageSource((Byte[])reader["Photo"]);
                }
                listPersonnes.Add(pers);
            }
            if (listPersonnes.Last().Territoires == null)
            {
                listPersonnes.Last().Territoires = new List <Territoire>();
            }
            var ter = new Territoire();

            ter.TerritoryID          = reader["TerritoryID"].ToString();
            ter.TerritoryDescription = reader["TerritoryDescription"].ToString();

            listPersonnes.Last().Territoires.Add(ter);
        }
Beispiel #2
0
        // Récupère les employés et leur territoires
        public static List <Employe> GetEmployesTerritoires()
        {
            List <Employe> lstEmployes = new List <Employe>();

            var    connectString = Properties.Settings.Default.NorthwindConnectString;
            string queryString   = @"select e.EmployeeID, LastName, FirstName, t.TerritoryID, t.TerritoryDescription
                        from Employees e
                        inner join EmployeeTerritories et on e.EmployeeID = et.EmployeeID
                        inner join Territories t on et.TerritoryID = t.TerritoryID
                        order by EmployeeID, TerritoryDescription";

            using (var connect = new SqlConnection(connectString))
            {
                var command = new SqlCommand(queryString, connect);
                connect.Open();

                var reader = command.ExecuteReader();

                while (reader.Read())
                {
                    int idEmpl = (int)reader["EmployeeID"];

                    // Si l'id de l'employe courant est != de celui du dernier de la liste,
                    // on crée un nouvel employé
                    Employe emp = null;
                    if (lstEmployes.Count == 0 || lstEmployes[lstEmployes.Count - 1].Id != idEmpl)
                    {
                        emp             = new Employe();
                        emp.Id          = (int)reader["EmployeeID"];
                        emp.Prenom      = (string)reader["FirstName"];
                        emp.Nom         = (string)reader["LastName"];
                        emp.Territoires = new List <Territoire>();

                        lstEmployes.Add(emp);
                    }
                    else
                    {
                        emp = lstEmployes[lstEmployes.Count - 1];
                    }

                    Territoire t = new Territoire();
                    t.Code        = (string)reader["TerritoryID"];
                    t.Description = (string)reader["TerritoryDescription"];
                    emp.Territoires.Add(t);
                }
            }

            return(lstEmployes);
        }