コード例 #1
0
ファイル: AlunoDao.cs プロジェクト: samuelmsilva83/Exemplos
        public List <Aluno> ConsultarTodosOsAlunos(String pesquisa)
        {
            try
            {
                var command = new SqlCommand();
                command.Connection = Conexao.connection;

                if (pesquisa == "")
                {
                    command.CommandText = @"SELECT * FROM ALUNOS order by nome";
                }
                else
                {
                    command.CommandText = @"SELECT * FROM ALUNOS 
                                            WHERE [id] = @ID
                                              or    [nome]			like @PESQUISA
                                              or    [dtnascimento]  like @PESQUISA
                                              or	[email]			like @PESQUISA
                                              or	[telefone]		like @PESQUISA
                                              or	[celular]		like @PESQUISA 
                                            order by nome
                                        ";

                    if (Char.IsDigit(pesquisa, 0))
                    {
                        command.Parameters.AddWithValue("@ID", Convert.ToInt32(pesquisa));
                    }
                    else
                    {
                        command.Parameters.AddWithValue("@ID", 0);
                    }

                    command.Parameters.AddWithValue("@PESQUISA", "'%" + pesquisa + "%'");
                }


                Conexao.Conectar();
                var reader = command.ExecuteReader();

                var alunos = new List <Aluno>();

                while (reader.Read())
                {
                    var aluno = new Aluno();

                    aluno.Id           = Convert.ToInt32(reader["id"]);
                    aluno.Nome         = reader["nome"].ToString();
                    aluno.DtNascimento = reader["dtnascimento"].ToString();
                    aluno.Email        = reader["email"].ToString();
                    aluno.Genero       = reader["genero"].ToString();
                    aluno.Telefone     = reader["telefone"].ToString();
                    aluno.Celular      = reader["celular"].ToString();
                    aluno.Observacao   = reader["observacao"].ToString();
                    aluno.Status       = reader["status"].ToString();

                    switch (reader["status"].ToString())
                    {
                    case "A":
                        aluno.ClasseStatus = "fa fa-power-off icone-acoes icone-aluno-ativo";
                        aluno.ToolTip      = "Desativar";
                        break;

                    case "I":
                        aluno.ClasseStatus = "fa fa-power-off icone-acoes icone-aluno-inativo";
                        aluno.ToolTip      = "Ativar";
                        break;

                    default:
                        aluno.ClasseStatus = "fa fa-power-off icone-acoes";
                        aluno.ToolTip      = "";
                        break;
                    }

                    aluno.DtCadastro      = reader["dt_cadastro"].ToString();
                    aluno.UsuarioCadastro = Convert.ToInt32(reader["usuario_cadastro"]);

                    alunos.Add(aluno);
                }
                reader.Close();

                return(alunos);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                Conexao.Desconectar();
            }
        }