JosueEguia

CapaDatos

Jan 9th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.34 KB | None | 0 0
  1. ===================================== CAPA DATOS ==============================================
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Data;
  9. using System.Data.SQLite;
  10.  
  11. namespace CapaDatos
  12. {
  13.     public class cdCortes
  14.     {
  15.  
  16.         //    CONEXION
  17.         private conexion Conexion = new conexion();
  18.         private SQLiteCommand Comando = new SQLiteCommand();
  19.         private SQLiteDataReader Leer;
  20.  
  21.         // MOSTRAR TABLA EN DATAGRIDVIEW (COMANDO EN SQL)
  22.         public DataTable Mostrar()
  23.         {
  24.             DataTable Tabla = new DataTable();
  25.             Comando.Connection = Conexion.AbrirConexion();
  26.             Comando.CommandText = "SELECT idcorte AS ID, cliente AS Cliente, fecha AS Fecha, cobro AS Cobro FROM cortes";
  27.             Comando.CommandType = CommandType.Text;
  28.             Leer = Comando.ExecuteReader();
  29.             Tabla.Load(Leer);
  30.             Leer.Close();
  31.             Conexion.CerrarConexion();
  32.             return Tabla;
  33.         }
  34.  
  35.         // INSERTAR DATOS EN LA BASE DE DATOS
  36.         // NO USAR STORAGE PROCEDURES AL SER SQLITE
  37.         public void Insertar (string cliente, string fecha, double cobro)
  38.         {
  39.             Comando.Connection = Conexion.AbrirConexion();
  40.             Comando.CommandText = "INSERT INTO cortes (cliente, fecha, cobro) VALUES (@cliente, @fecha, @cobro)";
  41.             Comando.CommandType = CommandType.Text;
  42.             //PARAMETROS PARA AGREGAR (VARIABLES EN LA BASE DE DATOS)
  43.             Comando.Parameters.AddWithValue("@cliente", cliente);
  44.             Comando.Parameters.AddWithValue("@fecha", fecha);
  45.             Comando.Parameters.AddWithValue("@cobro", cobro);
  46.             Comando.ExecuteNonQuery();
  47.             Comando.Parameters.Clear();
  48.         }
  49.  
  50.         // EDITAR DATOS EN LA BASE DE DATOS
  51.         // NO USAR STORAGE PROCEDURES AL SER SQLITE
  52.         public void Editar (string cliente, string fecha, double cobro, int idcorte)
  53.         {
  54.             Comando.Connection = Conexion.AbrirConexion();
  55.             Comando.CommandText = "UPDATE cortes SET cliente=@cliente, fecha=@fecha, cobro=@cobro WHERE idcorte=@idcorte";
  56.             Comando.CommandType = CommandType.Text;
  57.             //PARAMETROS PARA EDITAR (VARIABLES EN LA BASE DE DATOS)
  58.             Comando.Parameters.AddWithValue("@cliente", cliente);
  59.             Comando.Parameters.AddWithValue("@fecha", fecha);
  60.             Comando.Parameters.AddWithValue("@cobro", cobro);
  61.             Comando.Parameters.AddWithValue("@idcorte", idcorte);
  62.             Comando.ExecuteNonQuery();
  63.             Comando.Parameters.Clear();
  64.         }
  65.  
  66.         // ELIMINAR DATOS EN LA BASE DE DATOS
  67.         // NO USAR STORAGE PROCEDURES AL SER SQLITE
  68.         public void Eliminar (int idcorte)
  69.         {
  70.             Comando.Connection = Conexion.AbrirConexion();
  71.             Comando.CommandText = "delete from cortes where idcorte=@idcorte";
  72.             Comando.CommandType = CommandType.Text;
  73.             Comando.Parameters.AddWithValue("@idcorte", idcorte);
  74.             Comando.ExecuteNonQuery();
  75.             Comando.Parameters.Clear();
  76.         }
  77.  
  78.  
  79.     }
  80. }
  81.  
  82. ================================================== CAPA NEGOCIO =====================================================
  83.  
  84. using System;
  85. using System.Collections.Generic;
  86. using System.Linq;
  87. using System.Text;
  88. using System.Threading.Tasks;
  89. using System.Data;
  90. using System.Data.SQLite;
  91.  
  92. using CapaDatos;
  93.  
  94. namespace CapaNegocio
  95. {
  96.     public class cnCortes
  97.     {
  98.         private cdCortes objetoCorte = new cdCortes();
  99.         private cdCortes objetoSuma = new cdCortes();
  100.         private cdCortes objetoAgregar = new cdCortes();
  101.         private cdCortes objetoEditar = new cdCortes();
  102.         private cdCortes objetoEliminar = new cdCortes();
  103.  
  104.         public DataTable Mostrar()
  105.         {
  106.             DataTable tabla = new DataTable();
  107.             tabla = objetoCorte.Mostrar();
  108.             return tabla;
  109.         }
  110.  
  111.         public void AgregarCorte ( string cliente, string fecha, double cobro )
  112.         {
  113.             objetoAgregar.Insertar( cliente, fecha, Convert.ToDouble(cobro) );
  114.         }
  115.  
  116.         public void EditarCorte ( string cliente, string fecha, double cobro, int idcorte )
  117.         {
  118.             objetoEditar.Editar(cliente, fecha, Convert.ToDouble(cobro), Convert.ToInt32(idcorte));
  119.         }
  120.  
  121.         public void EliminarCorte( int idcorte)
  122.         {
  123.             objetoEliminar.Eliminar(Convert.ToInt32(idcorte));
  124.         }
  125.  
  126.     }
  127. }
  128.  
  129.  
  130. ========================================== CAPA PRESENTACION ============================================================
  131.  
  132. using System;
  133. using System.Collections.Generic;
  134. using System.ComponentModel;
  135. using System.Data;
  136. using System.Drawing;
  137. using System.Linq;
  138. using System.Text;
  139. using System.Threading.Tasks;
  140. using System.Windows.Forms;
  141.  
  142. using CapaNegocio;
  143.  
  144. namespace WindowsFormsApp2
  145. {
  146.     public partial class frmCortes : Form
  147.     {
  148.         cnCortes objetoCortes = new cnCortes();
  149.         cnCortes objetoSuma = new cnCortes();
  150.         cnCortes objetoAgregar = new cnCortes();
  151.         cnCortes objetoEditar = new cnCortes();
  152.         cnCortes objetoEliminar = new cnCortes();
  153.  
  154.         private string idcorte = null;
  155.         private bool Editar = false;
  156.  
  157.         public frmCortes()
  158.         {
  159.             InitializeComponent();
  160.         }
  161.  
  162.         public void Mostrar()
  163.         {
  164.             dataGridView1.DataSource = objetoCortes.Mostrar();
  165.         }
  166.  
  167.  
  168.         private void limpiar()
  169.         {
  170.             txtcliente.Text = "";
  171.             txtcobro.Text = "";
  172.         }
  173.  
  174.         private void frmCortes_Load(object sender, EventArgs e)
  175.         {
  176.             Mostrar();
  177.             dataGridView1.Columns[0].Visible = false;
  178.             dataGridView1.Columns["Cobro"].DefaultCellStyle.Format = "c";
  179.             sumar();
  180.         }
  181.  
  182.         private void button1_Click(object sender, EventArgs e)
  183.         {
  184.             try
  185.             {
  186.                 objetoAgregar.AgregarCorte(txtcliente.Text, Convert.ToString(dtpFecha.Value), Convert.ToDouble(txtcobro.Text));
  187.                 MessageBox.Show("El corte se ha guardado correctamente");
  188.                 Mostrar();
  189.                 limpiar();
  190.             }
  191.             catch (Exception ex)
  192.             {
  193.                 MessageBox.Show("El corte no se ha guardado correctamente: " + ex);
  194.             }
  195.         }
  196.  
  197.         private void button2_Click(object sender, EventArgs e)
  198.         {
  199.             try
  200.             {
  201.                 objetoEditar.EditarCorte(txtcliente.Text, Convert.ToString(dtpFecha.Value), Convert.ToDouble(txtcobro.Text), Convert.ToInt32(idcorte));
  202.                 MessageBox.Show("El corte se ha editado correctamente");
  203.                 Mostrar();
  204.                 limpiar();
  205.             }
  206.             catch (Exception ex)
  207.             {
  208.                 MessageBox.Show("El corte no se ha editado correctamente: " + ex);
  209.             }
  210.            
  211.         }
  212.  
  213.         private void button3_Click(object sender, EventArgs e)
  214.         {
  215.             if (dataGridView1.SelectedRows.Count > 0)
  216.             {
  217.                 idcorte = dataGridView1.CurrentRow.Cells[0].Value.ToString();
  218.                 objetoEliminar.EliminarCorte(Convert.ToInt32(idcorte));
  219.                 MessageBox.Show("El corte ha sido eliminado correctamente");
  220.                 Mostrar();
  221.             }
  222.             else
  223.                 MessageBox.Show("Seleccione una fila para eliminar");
  224.         }
  225.  
  226.         private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  227.         {
  228.             if (dataGridView1.SelectedRows.Count > 0)
  229.             {
  230.                 Editar = true;
  231.                 txtcliente.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
  232.                 dtpFecha.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
  233.                 txtcobro.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
  234.             }
  235.             else
  236.                 MessageBox.Show("Seleccione una fila para editar");
  237.         }
  238.  
  239.         public void sumar()
  240.         {
  241.             double total = 0;
  242.  
  243.             foreach (DataGridViewRow row in dataGridView1.Rows)
  244.             {
  245.                 total += Convert.ToDouble(row.Cells["Cobro"].Value);
  246.             }
  247.             lbltotal.Text = total.ToString();
  248.         }
  249.  
  250.  
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment