Implementação da Classe AlunoDAO em C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace AlunoBD
{
class AlunoDAO
{
public static void Insere(AlunoVO aluno)
{
AlunoVO pesquisado = AlunoDAO.Consultar(aluno.GetId());
if (pesquisado != null)
throw new Exception("Este ID de aluno já existe!");
string strMensalidade = aluno.GetMensalidade().ToString().Replace(',', '.');
string sql = string.Format("set dateformat dmy; " +
"Insert into alunos " +
"(id, nome, mensalidade, cidadeId, dataNascimento) " +
"values ({0}, '{1}', {2}, {3}, '{4}') ",
aluno.GetId(), aluno.GetNome(), strMensalidade,
aluno.GetCidadeId(), aluno.GetDataNascimento());
Metodos.ExecutaSQL(sql);
}
public static void Altera(AlunoVO aluno)
{
string strMensalidade = aluno.GetMensalidade().ToString().Replace(',', '.');
string sql = string.Format("set dateformat dmy; " +
"update alunos " +
"set nome = '{0}', mensalidade = {1}, cidadeId = {2}," +
"dataNascimento='{3}' where id = {4}",
aluno.GetNome(), strMensalidade,
aluno.GetCidadeId(), aluno.GetDataNascimento(),
aluno.GetId());
Metodos.ExecutaSQL(sql);
}
public static void Excluir(int id)
{
string sql = "delete alunos where id = " + id;
Metodos.ExecutaSQL(sql);
}
public static AlunoVO Consultar(int id)
{
string sql = "select * from alunos where id = " + id;
DataTable tabela = Metodos.ExecutaSelect(sql);
if (tabela != null)
{
DataRow registro = tabela.Rows[0];
AlunoVO aluno = new AlunoVO();
aluno.SetId(Convert.ToInt32(registro["id"]));
aluno.SetMensalidade(Convert.ToDouble(registro["mensalidade"]));
aluno.SetDataNascimento(Convert.ToDateTime(registro["datanascimento"]));
aluno.SetCidadeId(Convert.ToInt32(registro["CidadeId"]));
aluno.SetNome(registro["nome"].ToString());
return aluno;
}
else
return null;
}
public static AlunoVO MontaAlunoVO(DataRow registro)
{
AlunoVO aluno = new AlunoVO();
aluno.SetId(Convert.ToInt32(registro["id"]));
aluno.SetMensalidade(Convert.ToDouble(registro["mensalidade"]));
aluno.SetDataNascimento(Convert.ToDateTime(registro["datanascimento"]));
aluno.SetCidadeId(Convert.ToInt32(registro["CidadeId"]));
aluno.SetNome(registro["nome"].ToString());
return aluno;
}
public static AlunoVO Primeiro()
{
string sql = "select top 1 * from Alunos order by id";
DataTable tabela = Metodos.ExecutaSelect(sql);
if (tabela != null)
return MontaAlunoVO(tabela.Rows[0]);
else
return null;
}
public static AlunoVO Ultimo()
{
string sql = "select top 1 * from Alunos order by id desc";
DataTable tabela = Metodos.ExecutaSelect(sql);
if (tabela != null)
return MontaAlunoVO(tabela.Rows[0]);
else
return null;
}
public static AlunoVO Proximo(int IdAtual)
{
string sql = "select top 1 * from Alunos where Id > " + IdAtual + " order by id ";
DataTable tabela = Metodos.ExecutaSelect(sql);
if (tabela != null)
return MontaAlunoVO(tabela.Rows[0]);
else
return null;
}
public static AlunoVO Anterior(int IdAtual)
{
string sql = "select top 1 * from Alunos where Id <> " + IdAtual + " order by id desc";
DataTable tabela = Metodos.ExecutaSelect(sql);
if (tabela != null)
return MontaAlunoVO(tabela.Rows[0]);
else
return null;
}
public static int ProximoCodigoAluno()
{
string sql = "select ISNULL( MAX(id) +1 ,1) as 'maior' from Alunos";
DataTable tabela = Metodos.ExecutaSelect(sql);
return Convert.ToInt32(tabela.Rows[0]["maior"]);
}
public static DataTable ConsultarAlunos(
string nome, int codigoCidade
{
string sql = "select Alunos.*, cidades.nome as 'Nomecidade' " +
"from Alunos " +
"inner join cidades on cidades.id = Alunos.cidadeId ";
string filtro = "where 1 = 1 ";
if (nome.Length != 0)
filtro = string.Format(" and alunos.nome like '%{0}%' ", nome);
if (codigoCidade > 0)
filtro += " and cidadeId = " + codigoCidade;
sql = sql + filtro;
DataTable tabela = Metodos.ExecutaSelect(sql);
return tabela;
}
}
}