cdiego

DAOProducto

Jul 13th, 2013
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Web.Configuration;
  7. using System.Data;
  8. using ENTIDADES;
  9.  
  10. namespace DAO
  11. {
  12.     public class DAOProducto
  13.     {
  14.         private string cadena;
  15.  
  16.         public DAOProducto()
  17.         {
  18.             cadena = WebConfigurationManager.ConnectionStrings["cadena"].ConnectionString;
  19.         }
  20.  
  21.         public List<eProducto> ListarTodo()
  22.         {
  23.             SqlConnection con = new SqlConnection(cadena);
  24.             SqlCommand cmd = new SqlCommand("SP_PRODUCTO_LISTAR_TODO", con);
  25.             List<eProducto> lista = new List<eProducto>();
  26.             con.Open();
  27.             SqlDataReader lector = cmd.ExecuteReader();
  28.             while (lector.Read())
  29.             {
  30.                 eProducto p = new eProducto();
  31.                 p.IdProducto = lector.GetInt32(0);
  32.                 p.Nombre = lector.GetString(1);
  33.                 p.Precio = lector.GetDecimal(2);
  34.                 p.IdCategoria = lector.GetInt32(3);
  35.                 lista.Add(p);
  36.             }
  37.             con.Close();
  38.             return lista;
  39.         }
  40.         public List<eProducto> ListarPorCategoria(int IdCategoria)
  41.         {
  42.             SqlConnection con = new SqlConnection(cadena);
  43.             SqlCommand cmd = new SqlCommand("SP_PRODUCTO_LISTAR_TODO_POR_CATEGORIA", con);
  44.             cmd.CommandType = CommandType.StoredProcedure;
  45.             cmd.Parameters.Add("@pIdCategoria", SqlDbType.Int).Value = IdCategoria;
  46.             List<eProducto> lista = new List<eProducto>();
  47.             con.Open();
  48.             SqlDataReader lector = cmd.ExecuteReader();
  49.             while (lector.Read())
  50.             {
  51.                 eProducto p = new eProducto();
  52.                 p.IdProducto = lector.GetInt32(0);
  53.                 p.Nombre = lector.GetString(1);
  54.                 p.Precio = lector.GetDecimal(2);
  55.                 p.IdCategoria = lector.GetInt32(3);
  56.                 lista.Add(p);
  57.             }
  58.             con.Close();
  59.             return lista;
  60.         }
  61.         public bool Actualizar(eProducto Producto)
  62.         {
  63.             SqlConnection con = new SqlConnection(cadena);
  64.             SqlCommand cmd = new SqlCommand("SP_PRODUCTO_ACTUALIZAR", con);
  65.             cmd.CommandType = CommandType.StoredProcedure;
  66.             cmd.Parameters.Add("@pIdProducto", SqlDbType.Int).Value = Producto.IdProducto;
  67.             cmd.Parameters.Add("@pNombre", SqlDbType.VarChar).Value = Producto.Nombre;
  68.             cmd.Parameters.Add("@pPrecio", SqlDbType.Decimal).Value = Producto.Precio;
  69.             cmd.Parameters.Add("@pIdCategoria", SqlDbType.Int).Value = Producto.IdCategoria;
  70.             con.Open();
  71.             int exito = cmd.ExecuteNonQuery();
  72.             con.Close();
  73.             if (exito == 1)
  74.             {
  75.                 return true;
  76.             }
  77.             else
  78.             {
  79.                 return false;
  80.             }
  81.         }
  82.         public bool Grabar(eProducto Producto)
  83.         {
  84.             return false;
  85.         }
  86.  
  87.        
  88.  
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment