0% found this document useful (0 votes)
116 views7 pages

Crear: Crud/Abm Practica NOMBRE: Assad Joaquin Cadena Antonio

The document describes a CRUD application for managing client data in a SQL database. It includes classes for connecting to the database, performing CRUD operations on client data, and a form for viewing, adding, editing and deleting clients. The classes handle retrieving, adding, updating and deleting client records from the database. The form contains controls for searching, viewing, and modifying client data, as well as buttons to call the CRUD methods.

Uploaded by

Joaquin Cadena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views7 pages

Crear: Crud/Abm Practica NOMBRE: Assad Joaquin Cadena Antonio

The document describes a CRUD application for managing client data in a SQL database. It includes classes for connecting to the database, performing CRUD operations on client data, and a form for viewing, adding, editing and deleting clients. The classes handle retrieving, adding, updating and deleting client records from the database. The form contains controls for searching, viewing, and modifying client data, as well as buttons to call the CRUD methods.

Uploaded by

Joaquin Cadena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CRUD/ABM PRACTICA

NOMBRE: Assad Joaquin Cadena Antonio

1. Crear

2. Modificar.
3. Eliminar

Clase cliente
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRUD_ABM
{
class cliente
{
public string cod { get; set; }
public string nom { get; set; }
public string ape { get; set; }
public cliente() { }
public cliente(string vcod, string vnom, string vape)
{
this.cod = vcod;
this.nom = vnom;
this.nom = vape;
}
public static DataTable ListadoCliente()
{
using (SqlConnection con = conexion.ObtenerConeccion())
{
string queryString = "SELECT * from CLIENTE;";
SqlCommand command = new SqlCommand(queryString, con);
SqlDataReader reader = command.ExecuteReader();
DataTable tabla = new DataTable();
tabla.Load(reader);
con.Close();
return tabla;
}
}
public static DataTable BuscarCliente(string vcod)
{
using (SqlConnection con = conexion.ObtenerConeccion())
{
String queryString = "SELECT * from CLIENTE where CODCLI=@codX;";
SqlCommand command = new SqlCommand(queryString, con);
command.Parameters.AddWithValue("@codX", vcod);
SqlDataReader reader = command.ExecuteReader();
DataTable tabla = new DataTable();
tabla.Load(reader);
con.Close();
return tabla;
}
}
public static int AgragarCliente(cliente x)
{
int retorno = 0;
using (SqlConnection con = conexion.ObtenerConeccion())
{
string insertar = "Insert into CLIENTE values(@cod,@nom,@ape);";
SqlCommand comando = new SqlCommand(insertar, con);
comando.Parameters.AddWithValue("@cod", x.cod);
comando.Parameters.AddWithValue("@nom", x.nom);
comando.Parameters.AddWithValue("@ape", x.ape);
retorno = comando.ExecuteNonQuery();
con.Close();
}
return retorno;
}
public static int ModificarCliente(cliente x)
{
int retorno = 0;
using (SqlConnection con = conexion.ObtenerConeccion())
{
string update = "Update CLIENTE set NOMBRE=@nom,APELLIDO=@ape
where CODCLI=@cod;";
SqlCommand comando = new SqlCommand(update, con);
comando.Parameters.AddWithValue("@cod", x.cod);
comando.Parameters.AddWithValue("@nom", x.nom);
comando.Parameters.AddWithValue("@ape", x.ape);
retorno = comando.ExecuteNonQuery();
con.Close();
}
return retorno;
}
public static int EliminarCliente(string cod)
{
int retorno = 0;
using (SqlConnection con = conexion.ObtenerConeccion())
{
string query = "DELETE FROM CLIENTE WHERE CODCLI=@cod";
SqlCommand comando = new SqlCommand(query, con);
comando.Parameters.AddWithValue("@cod", cod);
retorno = comando.ExecuteNonQuery();
con.Close();
}
return retorno;
}

}
}

Clase conexión

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRUD_ABM
{
class conexion
{
public static SqlConnection ObtenerConeccion()
{
SqlConnection conection = new SqlConnection("Data
Source=(local);Initial Catalog=BDempre;Integrated Security=true");
conection.Open();
return conection;
}
}

Codigo formulario

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;

namespace CRUD_ABM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Boolean swNuevo;

void habilitar(Boolean sw)


{
GBbuscar.Enabled = sw;
GBdatos.Enabled = !sw;
GBacciones.Enabled = sw;
}

void limpiar()
{
TXTcod.Clear();
TXTnom.Clear();
TXTape.Clear();
}

private void GBdatos_Enter(object sender, EventArgs e)


{

private void BTNbuscar_Click(object sender, EventArgs e)


{
DataTable tabla = cliente.BuscarCliente(TXTbuscar.Text);
if (tabla.Rows.Count > 0)
{
DataRow fila = tabla.Rows[0];
TXTcod.Text = fila[0].ToString();
TXTnom.Text = fila[1].ToString();
TXTape.Text = fila[2].ToString();
DGVdatos.DataSource = tabla;
DGVdatos.Refresh();
}
else
MessageBox.Show("CODIGO NO ENCONTRADO", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

private void Form1_Load(object sender, EventArgs e)


{
habilitar(true);
DataTable tabla = cliente.ListadoCliente();
DGVdatos.DataSource = tabla;
DGVdatos.Refresh();
}

private void BTNnuevo_Click(object sender, EventArgs e)


{
swNuevo = true;
habilitar(false);
limpiar();
}

private void BTNmodificar_Click(object sender, EventArgs e)


{
if (TXTcod.Text.Trim() != "")
{
swNuevo = false;
habilitar(false);
}
else
MessageBox.Show("SIN DATOS", "ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

private void BTNgrabar_Click(object sender, EventArgs e)


{
cliente x = new cliente();
x.cod = TXTcod.Text;
x.nom = TXTnom.Text;
x.ape = TXTape.Text;
if (swNuevo == true)
{
if (cliente.AgragarCliente(x) > 0)
{
MessageBox.Show("REGISTRO EXITOSO", "MENSAJE",
MessageBoxButtons.OK, MessageBoxIcon.Error);
DataTable tabla = cliente.ListadoCliente();
DGVdatos.DataSource = tabla;
DGVdatos.Refresh();
}
}
else
{
if (cliente.ModificarCliente(x) > 0)
{
MessageBox.Show("REGISTRO EXITOSO", "MENSAJE",
MessageBoxButtons.OK, MessageBoxIcon.Information);
DataTable tabla = cliente.ListadoCliente();
DGVdatos.DataSource = tabla;
DGVdatos.Refresh();
}
}
habilitar(true);
}

private void BTNeliminar_Click(object sender, EventArgs e)


{
if (TXTcod.Text.Trim() != "")
{
if (MessageBox.Show("¿DESEA ELIMINAR EL REGISTRO?", "",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (cliente.EliminarCliente(TXTcod.Text) > 0)
{
DataTable table = cliente.ListadoCliente();
MessageBox.Show("REGISTRO ELIMINADO");
DGVdatos.DataSource = table;
DGVdatos.Refresh();
limpiar();
}
}
else
{
MessageBox.Show("CANCELADO");
}
}
}
private void BTNtodos_Click(object sender, EventArgs e)
{
Form1_Load(sender, e);
}

private void BTNcancelar_Click(object sender, EventArgs e)


{
habilitar(true);
limpiar();
}
}
}

CREACION SQL

CREATE DATABASE BDempre


USE BDempre
CREATE TABLE CLIENTE
(
CODCLI VARCHAR (50) NOT NULL PRIMARY KEY,
NOMBRE VARCHAR (50),
APELLIDO VARCHAR (50)
)

INSERT INTO CLIENTE VALUES ('C1', 'Carlos', 'Perez');


INSERT INTO CLIENTE VALUES ('C2', 'Ignacio', 'Valencia');
SELECT*FROM CLIENTE

You might also like