Ejemplo n.º 1
0
        public override List <modelocrud> recuperar(int?id)
        {
            Select_padrao = "select * from ReuniaoPessoa as RP ";
            if (id != null)
            {
                Select_padrao += $" where RP.Id='{id}'";
            }

            List <modelocrud> modelos = new List <modelocrud>();
            var conecta = bd.obterconexao();

            conecta.Open();
            SqlCommand    comando = new SqlCommand(Select_padrao, conecta);
            SqlDataReader dr      = comando.ExecuteReader();

            if (dr.HasRows == false)
            {
                bd.obterconexao().Close();
                return(modelos);
            }

            if (id != null)
            {
                try
                {
                    dr.Read();
                    this.ReuniaoId = int.Parse(Convert.ToString(dr["ReuniaoId"]));
                    this.PessoaId  = int.Parse(Convert.ToString(dr["PessoaId"]));
                    dr.Close();
                    modelos.Add(this);
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    bd.obterconexao().Close();
                }

                return(modelos);
            }
            else
            {
                try
                {
                    while (dr.Read())
                    {
                        ReuniaoPessoa pm = new ReuniaoPessoa();
                        pm.ReuniaoId = int.Parse(Convert.ToString(dr["ReuniaoId"]));
                        pm.PessoaId  = int.Parse(Convert.ToString(dr["PessoaId"]));
                        modelos.Add(pm);
                    }
                    dr.Close();

                    var pessoas  = Pessoa.recuperarTodos().OfType <Pessoa>().ToList();
                    var reunioes = new Reuniao().recuperar(null).OfType <Reuniao>().ToList();
                    foreach (var item in modelos.OfType <ReuniaoPessoa>().ToList())
                    {
                        item.Pessoa  = pessoas.First(i => i.IdPessoa == item.PessoaId);
                        item.Reuniao = reunioes.First(i => i.IdReuniao == item.ReuniaoId);
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    bd.obterconexao().Close();
                }
                return(modelos);
            }
        }