Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BOO 5.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5.  
  6. namespace agenadaAspnet
  7. {
  8.     public class Contato
  9.     {
  10.         public int Id { get; set; }
  11.         public string Nome { get; set; }
  12.         public DateTime Nascimento { get; set; }
  13.         public string Telefone { get; set; }
  14.         public string Cidade { get; set; }
  15.         public string Estado { get; set; }
  16.  
  17.         public bool Validar()
  18.         {
  19.             if (string.IsNullOrEmpty(Nome)) return false;
  20.             else if (string.IsNullOrEmpty(Telefone)) return false;
  21.             else if (string.IsNullOrEmpty(Cidade)) return false;
  22.             else if (string.IsNullOrEmpty(Estado)) return false;
  23.             else if (Nascimento == null) return false;
  24.             else return true;
  25.         }
  26.  
  27.         public string Inserir()
  28.         {
  29.             string msg = string.Empty;
  30.             Banco bd = new Banco();
  31.  
  32.             var sql = "INSERT INTO Contatos VALUES (@Nome,@Telefone,@Cidade,@Nascimento,@Estado)";
  33.  
  34.             List<SqlParameter> paramentros = new List<SqlParameter>();
  35.             paramentros.Add(new SqlParameter("@Nome", this.Nome));
  36.             paramentros.Add(new SqlParameter("@Telefone", this.Telefone));
  37.             paramentros.Add(new SqlParameter("@Telefone", this.Telefone));
  38.             paramentros.Add(new SqlParameter("@Cidade", this.Cidade));
  39.             paramentros.Add(new SqlParameter("@Nascimento", this.Nascimento));
  40.             paramentros.Add(new SqlParameter("@Estado", this.Estado));
  41.  
  42.             try
  43.             {
  44.                 msg = bd.ExecutarComando(sql, paramentros) > 0 ? "Inserido com sucesso!" : "Não inserido!";
  45.             }
  46.             catch (Exception ex)
  47.             {
  48.                 msg = ex.Message;
  49.             }
  50.             return msg;
  51.         }
  52.  
  53.         public string Deletar()
  54.         {
  55.             return "Deletado com sucesso!";
  56.         }
  57.  
  58.         public string Atualizar()
  59.         {
  60.             return "Inserido com sucesso!";
  61.         }
  62.  
  63.         public List<Contato> Selecionar()
  64.         {
  65.             return new List<Contato>();
  66.         }
  67.     }
  68.  
  69.  
  70.     public class Banco : IBanco
  71.     {
  72.         public static SqlConnection Conexao;
  73.  
  74.         public Banco()
  75.         {
  76.             if (Conexao == null)
  77.                 Conexao = new SqlConnection(@"Data Source=.\SQLEXPRESS; Initial Catalog=dbTeste; Integrated Security=true");
  78.         }
  79.  
  80.         public void Conectar()
  81.         {
  82.             try
  83.             {
  84.                 Conexao.Open();
  85.             }
  86.             catch (Exception ex)
  87.             {
  88.                 throw ex;
  89.             }
  90.  
  91.         }
  92.  
  93.         public void Desconectar()
  94.         {
  95.             if (Conexao.State != ConnectionState.Closed)
  96.                 try
  97.                 {
  98.                     Conexao.Close();
  99.                 }
  100.                 catch (Exception ex)
  101.                 {
  102.                     throw ex;
  103.                 }
  104.         }
  105.  
  106.         public int ExecutarComando(string sql, List<SqlParameter> parametros)
  107.         {
  108.             int linhasAfetadas = 0;
  109.             using (SqlCommand cmd = new SqlCommand(sql, Conexao))
  110.             {
  111.                 foreach (var parametro in parametros)
  112.                     cmd.Parameters.Add(parametro);
  113.  
  114.                 try
  115.                 {
  116.                     Conectar();
  117.                     linhasAfetadas = cmd.ExecuteNonQuery();
  118.                 }
  119.                 catch (Exception ex)
  120.                 {
  121.                     throw ex;
  122.                 }
  123.                 finally
  124.                 {
  125.                     Desconectar();
  126.                 }
  127.             }
  128.             return linhasAfetadas;
  129.         }
  130.  
  131.         public List<Contato> Select(string sql, List<SqlParameter> parametros)
  132.         {
  133.             var Contatos = new List<Contato>();
  134.             using (SqlDataAdapter cmd = new SqlDataAdapter(sql, Conexao))
  135.             {
  136.                 foreach (var parametro in parametros)
  137.                     cmd.SelectCommand.Parameters.Add(parametro);
  138.  
  139.                 try
  140.                 {
  141.                     Conectar();
  142.                     DataTable dt = new DataTable("Clientes");
  143.                     cmd.Fill(dt);
  144.  
  145.                     foreach (DataRow item in dt.Rows)
  146.                     {
  147.                         Contatos.Add(new Contato()
  148.                         {
  149.                             Id = int.Parse(item["Id"].ToString()),
  150.                             Nascimento = DateTime.Parse(item["Nascimento"].ToString()),
  151.                             Estado = item["Estado"].ToString(),
  152.                             Cidade = item["Cidade"].ToString(),
  153.                             Nome = item["Nome"].ToString(),
  154.                             Telefone = item["Telefone"].ToString()
  155.                         });
  156.                     }
  157.                 }
  158.                 catch (Exception ex)
  159.                 {
  160.                     throw ex;
  161.                 }
  162.                 finally
  163.                 {
  164.                     Desconectar();
  165.                 }
  166.             }
  167.             return Contatos;
  168.         }
  169.     }
  170.  
  171.     public interface IBanco
  172.     {
  173.         void Conectar();
  174.         void Desconectar();
  175.         int ExecutarComando(string sql, List<SqlParameter> parametros);
  176.         List<Contato> Select(string sql, List<SqlParameter> parametros);
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement