Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- namespace agenadaAspnet
- {
- public class Contato
- {
- public int Id { get; set; }
- public string Nome { get; set; }
- public DateTime Nascimento { get; set; }
- public string Telefone { get; set; }
- public string Cidade { get; set; }
- public string Estado { get; set; }
- public bool Validar()
- {
- if (string.IsNullOrEmpty(Nome)) return false;
- else if (string.IsNullOrEmpty(Telefone)) return false;
- else if (string.IsNullOrEmpty(Cidade)) return false;
- else if (string.IsNullOrEmpty(Estado)) return false;
- else if (Nascimento == null) return false;
- else return true;
- }
- public string Inserir()
- {
- string msg = string.Empty;
- Banco bd = new Banco();
- var sql = "INSERT INTO Contatos VALUES (@Nome,@Telefone,@Cidade,@Nascimento,@Estado)";
- List<SqlParameter> paramentros = new List<SqlParameter>();
- paramentros.Add(new SqlParameter("@Nome", this.Nome));
- paramentros.Add(new SqlParameter("@Telefone", this.Telefone));
- paramentros.Add(new SqlParameter("@Telefone", this.Telefone));
- paramentros.Add(new SqlParameter("@Cidade", this.Cidade));
- paramentros.Add(new SqlParameter("@Nascimento", this.Nascimento));
- paramentros.Add(new SqlParameter("@Estado", this.Estado));
- try
- {
- msg = bd.ExecutarComando(sql, paramentros) > 0 ? "Inserido com sucesso!" : "Não inserido!";
- }
- catch (Exception ex)
- {
- msg = ex.Message;
- }
- return msg;
- }
- public string Deletar()
- {
- return "Deletado com sucesso!";
- }
- public string Atualizar()
- {
- return "Inserido com sucesso!";
- }
- public List<Contato> Selecionar()
- {
- return new List<Contato>();
- }
- }
- public class Banco : IBanco
- {
- public static SqlConnection Conexao;
- public Banco()
- {
- if (Conexao == null)
- Conexao = new SqlConnection(@"Data Source=.\SQLEXPRESS; Initial Catalog=dbTeste; Integrated Security=true");
- }
- public void Conectar()
- {
- try
- {
- Conexao.Open();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Desconectar()
- {
- if (Conexao.State != ConnectionState.Closed)
- try
- {
- Conexao.Close();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public int ExecutarComando(string sql, List<SqlParameter> parametros)
- {
- int linhasAfetadas = 0;
- using (SqlCommand cmd = new SqlCommand(sql, Conexao))
- {
- foreach (var parametro in parametros)
- cmd.Parameters.Add(parametro);
- try
- {
- Conectar();
- linhasAfetadas = cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- Desconectar();
- }
- }
- return linhasAfetadas;
- }
- public List<Contato> Select(string sql, List<SqlParameter> parametros)
- {
- var Contatos = new List<Contato>();
- using (SqlDataAdapter cmd = new SqlDataAdapter(sql, Conexao))
- {
- foreach (var parametro in parametros)
- cmd.SelectCommand.Parameters.Add(parametro);
- try
- {
- Conectar();
- DataTable dt = new DataTable("Clientes");
- cmd.Fill(dt);
- foreach (DataRow item in dt.Rows)
- {
- Contatos.Add(new Contato()
- {
- Id = int.Parse(item["Id"].ToString()),
- Nascimento = DateTime.Parse(item["Nascimento"].ToString()),
- Estado = item["Estado"].ToString(),
- Cidade = item["Cidade"].ToString(),
- Nome = item["Nome"].ToString(),
- Telefone = item["Telefone"].ToString()
- });
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- Desconectar();
- }
- }
- return Contatos;
- }
- }
- public interface IBanco
- {
- void Conectar();
- void Desconectar();
- int ExecutarComando(string sql, List<SqlParameter> parametros);
- List<Contato> Select(string sql, List<SqlParameter> parametros);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement