Está en la página 1de 11

PRACTICA 4.

PROGRAMACIÓN CLIENTE/SERVIDOR-CRUD CON MANEJO DE TRANSACCIONES


GUIA 2
BASE DE DATOS II
F
Nombre: JHAZIEL SARAI SANDOVAL MAMANI

Crear dos formularios:


FRMLISTA

FRMDATOS
1. Procedimientos almacenados en SQL server
////LISTAR EMPLEADO
create procedure ListarE
as
select * from empleado
exec ListarE

////ELIMINAR EMPLEADO
create procedure EliminarE
@cod varchar (10)
as
begin tran EliminarT
begin try
delete from empleado where codigo=@cod
commit tran EliminarT
print 'eliminado'
end try
begin catch
print @@error
rollback tran EliminarT
end catch
go

///AGREGAR EMPLEADO
create procedure AgregarE
@cod varchar (10),
@ape varchar(15),
@nom varchar(15)
as
begin tran AgregarT
begin try
insert into empleado values (@cod, @ape, @nom)
commit tran AgregarT
print 'registrado'
end try
begin catch
print @@error
rollback tran AgregarT
end catch
go
exec AgregarE 'E5','joey','styles'
select * from empleado

/// ACTUALIZAR EMPLEADO


create procedure ActualizarE
@cod varchar (10),
@ape varchar(15),
@nom varchar(15)
as
begin tran ActualizarT
begin try
update empleado set apellido=@ape, nombre=@nom where codigo=@cod
commit tran ActualizarT
print 'actualizado'
end try
begin catch
print @@error
rollback tran ActualizarT
end catch
go
exec ActualizarE 'E5',’JHAZIEL’,'SANDOVAL'

2. Clase Empleado
class empleado
{
public string cod { get; set; }
public string ape { get; set; }
public string nom { get; set; }
public empleado() { }
public empleado(string vcod, string vape, string vnom)
{
this.cod = vcod;
this.ape = vape;
this.nom = vnom;
}
public static DataTable ListadoEmpleado()
{
using (SqlConnection con = conexion.ObtenerConexion())
{
SqlCommand command = new SqlCommand("ListarE", con);
command.CommandType = CommandType.StoredProcedure;
DataSet datos = new DataSet();
SqlDataAdapter adaptador = new SqlDataAdapter();
adaptador.SelectCommand = command;
adaptador.Fill(datos, "empleado");
con.Close();
DataTable tabla = datos.Tables["empleado"];
return tabla;
}

public static int AgregarEmpleado(empleado x)


{
int retorno = 0;
using (SqlConnection con = conexion.ObtenerConexion())
{
SqlCommand comando = new SqlCommand("AgregarE", con);
comando.CommandType = CommandType.StoredProcedure;
comando.Parameters.Add("@cod", SqlDbType.VarChar, 10);
comando.Parameters.Add("@ape", SqlDbType.VarChar, 15);
comando.Parameters.Add("@nom", SqlDbType.VarChar, 15);
comando.Parameters["@cod"].Value = x.cod;
comando.Parameters["@ape"].Value = x.ape;
comando.Parameters["@nom"].Value = x.nom;
DataSet datos = new DataSet();
SqlDataAdapter adaptador = new SqlDataAdapter();
adaptador.SelectCommand = comando;
adaptador.Fill(datos, "empleado");
con.Close();
DataTable tabla = datos.Tables["empleado"];
retorno = 1;
}
return retorno;
}
public static int ModificarEmpleado(empleado x)
{
int retorno = 0;
using (SqlConnection con = conexion.ObtenerConexion())
{
SqlCommand comando = new SqlCommand("ActualizarE", con);
comando.CommandType = CommandType.StoredProcedure;
comando.Parameters.Add("@cod", SqlDbType.VarChar, 10);
comando.Parameters.Add("@ape", SqlDbType.VarChar, 15);
comando.Parameters.Add("@nom", SqlDbType.VarChar, 15);
comando.Parameters["@cod"].Value = x.cod;
comando.Parameters["@ape"].Value = x.ape;
comando.Parameters["@nom"].Value = x.nom;
DataSet datos = new DataSet();
SqlDataAdapter adaptador = new SqlDataAdapter();
adaptador.SelectCommand = comando;
adaptador.Fill(datos, "empleado");
con.Close();
DataTable tabla = datos.Tables["empleado"];
retorno = 1;
}
return retorno;
}
public static int EliminarEmpleado(int vcod)
{

int retorno = 0;
using (SqlConnection con = conexion.ObtenerConexion())
{
SqlCommand comando = new SqlCommand("EliminarE", con);
comando.CommandType = CommandType.StoredProcedure;
comando.Parameters.Add("@cod", SqlDbType.VarChar, 3);
comando.Parameters["@cod"].Value = vcod;
DataSet datos = new DataSet();
SqlDataAdapter adaptador = new SqlDataAdapter();
adaptador.SelectCommand = comando;
adaptador.Fill(datos, "empleado");
con.Close();
DataTable tabla = datos.Tables["empleado"];
retorno = 1;
//return tabla;
}
return retorno;
}
}

3. Código de los dos formularios

FRMLISTA.-

public partial class FRMLista : Form


{
public FRMLista()
{
InitializeComponent();
}
Boolean swNuevo=true;
private void BTNeliminar_Click(object sender, EventArgs e)
{
if (MessageBox.Show("¿DESEA ELIMINAR EL REGISTRO?", "",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (DGVdatos.Rows.Count > 0)
{
int fila = DGVdatos.CurrentRow.Index;
int codigo;
codigo = int.Parse(DGVdatos.Rows[fila].Cells[0].Value.ToString());
empleado.EliminarEmpleado(codigo);
DataTable tabla = empleado.ListadoEmpleado();
DGVdatos.DataSource = tabla;
DGVdatos.Refresh();
}
else
{
MessageBox.Show("No hay empleados que eliminar!!!!!!!");
}
}
else
{
MessageBox.Show("cancelado");
}

private void FRMLista_Load(object sender, EventArgs e)


{
try
{
DataTable tabla = empleado.ListadoEmpleado();
DGVdatos.DataSource = tabla;
DGVdatos.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

private void BTNnuevo_Click(object sender, EventArgs e)


{
this.Hide();
FRMdatos FRMdatos = new FRMdatos();
FRMdatos.Show();
}

private void BTNeditar_Click(object sender, EventArgs e)


{
if (DGVdatos.Rows.Count > 0)
{
int fila = DGVdatos.CurrentRow.Index;
string codigo;
codigo = DGVdatos.Rows[fila].Cells[0].Value.ToString();
FRMdatos abrir = new FRMdatos(codigo);
abrir.Show();
this.Hide();
}
else
{
MessageBox.Show("No hay empleados que modificar!!!!!!!");
}

private void BTNtodos_Click(object sender, EventArgs e)


{
FRMLista_Load(sender, e);
}
}
}

FRMDATOS

public partial class FRMdatos : Form

int NRO;

Boolean swNuevo;

public FRMdatos()

InitializeComponent();

swNuevo = true;

}
public FRMdatos(string num)

NRO = Convert.ToInt32(num);

swNuevo = true;

private void FRMdatos_Load_1(object sender, EventArgs e)

private void BTNatras_Click(object sender, EventArgs e)

this.Hide();

FRMLista Form3 = new FRMLista();

Form3.Show();

private void BTNaceptar_Click(object sender, EventArgs e)

empleado x = new empleado();

x.cod = TXTcod.Text;

x.ape = TXTape.Text;

x.nom = TXTnom.Text;

if (swNuevo == true)

try

if (empleado.AgregarEmpleado(x) > 0)

MessageBox.Show("REGISTRO EXITOSO", "MESSAJE", MessageBoxButtons.OK,


MessageBoxIcon.Information);

DataTable tabla = empleado.ListadoEmpleado();

this.Hide();

FRMLista FRMLista = new FRMLista();

FRMLista.Show();
}

catch (Exception ex)

MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK,


MessageBoxIcon.Error);

else

try

if (empleado.ModificarEmpleado(x) > 0)

MessageBox.Show("REGISTRO EXITOSO", "MESSAJE", MessageBoxButtons.OK,


MessageBoxIcon.Information);

DataTable tabla = empleado.ListadoEmpleado();

this.Hide();

FRMLista FRMLista = new FRMLista();

FRMLista.Show();

catch (Exception ex)

MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK,


MessageBoxIcon.Error);

AGREGAR
MODIFICAR
ELIMINAR

También podría gustarte