Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ===================================== CAPA DATOS ==============================================
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.Data.SQLite;
- namespace CapaDatos
- {
- public class cdCortes
- {
- // CONEXION
- private conexion Conexion = new conexion();
- private SQLiteCommand Comando = new SQLiteCommand();
- private SQLiteDataReader Leer;
- // MOSTRAR TABLA EN DATAGRIDVIEW (COMANDO EN SQL)
- public DataTable Mostrar()
- {
- DataTable Tabla = new DataTable();
- Comando.Connection = Conexion.AbrirConexion();
- Comando.CommandText = "SELECT idcorte AS ID, cliente AS Cliente, fecha AS Fecha, cobro AS Cobro FROM cortes";
- Comando.CommandType = CommandType.Text;
- Leer = Comando.ExecuteReader();
- Tabla.Load(Leer);
- Leer.Close();
- Conexion.CerrarConexion();
- return Tabla;
- }
- // INSERTAR DATOS EN LA BASE DE DATOS
- // NO USAR STORAGE PROCEDURES AL SER SQLITE
- public void Insertar (string cliente, string fecha, double cobro)
- {
- Comando.Connection = Conexion.AbrirConexion();
- Comando.CommandText = "INSERT INTO cortes (cliente, fecha, cobro) VALUES (@cliente, @fecha, @cobro)";
- Comando.CommandType = CommandType.Text;
- //PARAMETROS PARA AGREGAR (VARIABLES EN LA BASE DE DATOS)
- Comando.Parameters.AddWithValue("@cliente", cliente);
- Comando.Parameters.AddWithValue("@fecha", fecha);
- Comando.Parameters.AddWithValue("@cobro", cobro);
- Comando.ExecuteNonQuery();
- Comando.Parameters.Clear();
- }
- // EDITAR DATOS EN LA BASE DE DATOS
- // NO USAR STORAGE PROCEDURES AL SER SQLITE
- public void Editar (string cliente, string fecha, double cobro, int idcorte)
- {
- Comando.Connection = Conexion.AbrirConexion();
- Comando.CommandText = "UPDATE cortes SET cliente=@cliente, fecha=@fecha, cobro=@cobro WHERE idcorte=@idcorte";
- Comando.CommandType = CommandType.Text;
- //PARAMETROS PARA EDITAR (VARIABLES EN LA BASE DE DATOS)
- Comando.Parameters.AddWithValue("@cliente", cliente);
- Comando.Parameters.AddWithValue("@fecha", fecha);
- Comando.Parameters.AddWithValue("@cobro", cobro);
- Comando.Parameters.AddWithValue("@idcorte", idcorte);
- Comando.ExecuteNonQuery();
- Comando.Parameters.Clear();
- }
- // ELIMINAR DATOS EN LA BASE DE DATOS
- // NO USAR STORAGE PROCEDURES AL SER SQLITE
- public void Eliminar (int idcorte)
- {
- Comando.Connection = Conexion.AbrirConexion();
- Comando.CommandText = "delete from cortes where idcorte=@idcorte";
- Comando.CommandType = CommandType.Text;
- Comando.Parameters.AddWithValue("@idcorte", idcorte);
- Comando.ExecuteNonQuery();
- Comando.Parameters.Clear();
- }
- }
- }
- ================================================== CAPA NEGOCIO =====================================================
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.Data.SQLite;
- using CapaDatos;
- namespace CapaNegocio
- {
- public class cnCortes
- {
- private cdCortes objetoCorte = new cdCortes();
- private cdCortes objetoSuma = new cdCortes();
- private cdCortes objetoAgregar = new cdCortes();
- private cdCortes objetoEditar = new cdCortes();
- private cdCortes objetoEliminar = new cdCortes();
- public DataTable Mostrar()
- {
- DataTable tabla = new DataTable();
- tabla = objetoCorte.Mostrar();
- return tabla;
- }
- public void AgregarCorte ( string cliente, string fecha, double cobro )
- {
- objetoAgregar.Insertar( cliente, fecha, Convert.ToDouble(cobro) );
- }
- public void EditarCorte ( string cliente, string fecha, double cobro, int idcorte )
- {
- objetoEditar.Editar(cliente, fecha, Convert.ToDouble(cobro), Convert.ToInt32(idcorte));
- }
- public void EliminarCorte( int idcorte)
- {
- objetoEliminar.Eliminar(Convert.ToInt32(idcorte));
- }
- }
- }
- ========================================== CAPA PRESENTACION ============================================================
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using CapaNegocio;
- namespace WindowsFormsApp2
- {
- public partial class frmCortes : Form
- {
- cnCortes objetoCortes = new cnCortes();
- cnCortes objetoSuma = new cnCortes();
- cnCortes objetoAgregar = new cnCortes();
- cnCortes objetoEditar = new cnCortes();
- cnCortes objetoEliminar = new cnCortes();
- private string idcorte = null;
- private bool Editar = false;
- public frmCortes()
- {
- InitializeComponent();
- }
- public void Mostrar()
- {
- dataGridView1.DataSource = objetoCortes.Mostrar();
- }
- private void limpiar()
- {
- txtcliente.Text = "";
- txtcobro.Text = "";
- }
- private void frmCortes_Load(object sender, EventArgs e)
- {
- Mostrar();
- dataGridView1.Columns[0].Visible = false;
- dataGridView1.Columns["Cobro"].DefaultCellStyle.Format = "c";
- sumar();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- objetoAgregar.AgregarCorte(txtcliente.Text, Convert.ToString(dtpFecha.Value), Convert.ToDouble(txtcobro.Text));
- MessageBox.Show("El corte se ha guardado correctamente");
- Mostrar();
- limpiar();
- }
- catch (Exception ex)
- {
- MessageBox.Show("El corte no se ha guardado correctamente: " + ex);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- objetoEditar.EditarCorte(txtcliente.Text, Convert.ToString(dtpFecha.Value), Convert.ToDouble(txtcobro.Text), Convert.ToInt32(idcorte));
- MessageBox.Show("El corte se ha editado correctamente");
- Mostrar();
- limpiar();
- }
- catch (Exception ex)
- {
- MessageBox.Show("El corte no se ha editado correctamente: " + ex);
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (dataGridView1.SelectedRows.Count > 0)
- {
- idcorte = dataGridView1.CurrentRow.Cells[0].Value.ToString();
- objetoEliminar.EliminarCorte(Convert.ToInt32(idcorte));
- MessageBox.Show("El corte ha sido eliminado correctamente");
- Mostrar();
- }
- else
- MessageBox.Show("Seleccione una fila para eliminar");
- }
- private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
- {
- if (dataGridView1.SelectedRows.Count > 0)
- {
- Editar = true;
- txtcliente.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
- dtpFecha.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
- txtcobro.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
- }
- else
- MessageBox.Show("Seleccione una fila para editar");
- }
- public void sumar()
- {
- double total = 0;
- foreach (DataGridViewRow row in dataGridView1.Rows)
- {
- total += Convert.ToDouble(row.Cells["Cobro"].Value);
- }
- lbltotal.Text = total.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment