Código C# de Cadastro de Alunos com Conexão SQL Server

Artes plásticas e educação artística3,92 KB

class AlunoVO
{
private int id;
private string nome;
private double mensalidade;
private int cidadeId;
private DateTime dataNascimento;
// ID
public void SetId(int valor)
{
if (valor < 0)
throw new Exception("Id não pode ser negativo!");
else
id = valor;
}
public int GetId()
{
return id;
}
//Nome
public void SetNome(string valor)
{
if (valor.Length == 0)
throw new Exception("Informe o nome!");
else
nome = valor;
}
public string GetNome()
{
return nome;
}

//DataNascimento
public void SetDataNascimento(DateTime valor)
{
if (valor > DateTime.Now)
throw new Exception("Mas a pessoa nem nasceu ainda!");
else
dataNascimento = valor;
}
public DateTime GetDataNascimento()
{
return dataNascimento;
}
}



using System.Data.SqlClient;
namespace CadastroAluno_WF
{
class ConexaoBD
{
///
/// Método Estático que retorna um conexão aberta com o BD
///
/// Conexão aberta
public static SqlConnection GetConexao()
{
string strCon = "Data Source=LOCALHOST;Initial Catalog=AULADB;Integrated Security=True";
SqlConnection conexao = new SqlConnection(strCon);
conexao.Open();
return conexao;
}
}
}

using System.Data.SqlClient;
namespace CadastroAluno_WF
{
class AlunoDAO
{

// objeto aluno com todos os atributos preenchidos
public static void Inserir(AlunoVO aluno)
{
SqlConnection conexao = ConexaoBD.GetConexao();
try
{
// devemos substituir a ',' por '.'
string mensalidade = aluno.GetMensalidade().ToString().Replace(',', '.');
// set dateformat dmy; este comando serve para alterar a
// forma como o SQL Server entende o formato de data
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(), mensalidade, aluno.GetCidadeId(),
aluno.GetDataNascimento());
SqlCommand comando = new SqlCommand(sql, conexao);
comando.ExecuteNonQuery();
}
finally
{
conexao.Close();
}
}


public static void Alterar(AlunoVO aluno)
{
SqlConnection conexao = ConexaoBD.GetConexao();
try
{
// devemos substituir a ',' por '.'
string mensalidade = aluno.GetMensalidade().ToString().Replace(',', '.');
// set dateformat dmy; este comando serve para alterar a
// forma como o SQL Server entende o formato de data

string sql = String.Format("set dateformat dmy; " +
"update alunos set nome = '{0}', mensalidade = {1}, cidadeId = {2}," +
"dataNascimento = '{3}' Where id = {4}",
aluno.GetNome(), mensalidade,
aluno.GetCidadeId(), aluno.GetDataNascimento(), aluno.GetId());
SqlCommand comando = new SqlCommand(sql, conexao);
comando.ExecuteNonQuery();
}
finally
{
conexao.Close();
}
}

// EXCLUIR------------------------------------------------------------------------

public static void Excluir(int alunoId)
{
SqlConnection conexao = ConexaoBD.GetConexao();
try
{
string sql = "delete from alunos where id = " + alunoId.ToString();
SqlCommand comando = new SqlCommand(sql, conexao);
comando.ExecuteNonQuery();
}
finally
{
conexao.Close();
}
}



public static DataTable ExecutaSelect(string sql)
{
SqlConnection conexao = ConexaoBD.GetConexao();
try
{
SqlDataAdapter adapter = new SqlDataAdapter(sql, conexao);
DataTable tabelaTemp = new DataTable();
adapter.Fill(tabelaTemp);
return tabelaTemp;
}
finally
{
conexao.Close();
}
}

Baixar

PDF Word HTML