GuiaLab pjManttoContratista
GuiaLab pjManttoContratista
GuiaLab pjManttoContratista
GUIA DE LABORATORIO:
MANTENIMIENTO DE LA TABLA: CONTRATISTA
2017
Página 1 de 17
Implementar una aplicación Windows Forms que permita realizar el mantenimiento de registro de la tabla
Contratista.
Implementar en SQL Server procedimientos almacenados que permitan listar, insertar, modificar y eliminar
un registro de la tabla contratista.
Crear la clase Conexión e implementar el método getConecta, el cual permita asociarse a la cadena de
conexión especificada en el archivo app.config.
Crear la clase LogicaNegocio e implementar los métodos necesarios para el mantenimiento de registro de
contratistas.
Página 2 de 17
GUI propuesto para las opciones del menú:
Página 3 de 17
Formulario de actualización de datos del contratista.
Página 4 de 17
Solución:
Página 5 de 17
4.- Diagrama de relaciones:
Página 6 de 17
-- PROCEDIMIENTO ALMACENADO QUE LISTA LOS CONTRATISTAS
IF OBJECT_ID ('SP_LISTACONTRATISTA') IS NOT NULL
DROP PROC SP_LISTACONTRATISTA
GO
CREATE PROC SP_LISTACONTRATISTA
AS
SELECT
C.IDE_CON AS CODIGO,
C.NOM_CON+' '+C.PAT_CON+' '+C.MAT_CON AS CONTRATISTA,
C.FON_CON AS TELEFONO,
C.EMA_CON AS CORREO
FROM CONTRATISTA C
GO
GO
CREATE PROC SP_ULTIMOCODIGO
AS
SELECT TOP 1
C.IDE_CON AS CODIGO FROM CONTRATISTA C ORDER BY C.IDE_CON DESC
GO
Página 7 de 17
-- PROCEDIMIENTO ALMACENADO QUE PERMITE ELIMINAR
-- UN REGISTRO DE CONTRATISTA
IF OBJECT_ID('SP_ELIMINACONTRATISTA') IS NOT NULL
DROP PROC SP_ELIMINACONTRATISTA
GO
CREATE PROC SP_ELIMINACONTRATISTA
(
@COD CHAR(6)
)
AS
DELETE CONTRATISTA WHERE IDE_CON = @COD
GO
GO
CREATE PROC SP_BUSCACONTRATISTA
(
@COD CHAR(6)
)
AS
SELECT
C.IDE_CON AS CODIGO,
C.NOM_CON AS NOMBRE,
C.PAT_CON AS PATERNO,
C.MAT_CON AS MATERNO,
C.FON_CON AS TELEFONO,
C.EMA_CON AS CORREO
FROM CONTRATISTA C WHERE IDE_CON = @COD
GO
8.- Seleccionar Archivo > Nuevo Proyecto > Visual C# > Windows > Aplicación Windows Forms y asignar
por nombre: pjMantenimientoContratista.
System.Configuration
Página 8 de 17
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="cn"
connectionString="Data Source=.;Initial Catalog=CONTRATO;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Clase Conexión
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace pjMantenimientoContratista
{
public class Conexion
{
public SqlConnection getConecta()
{
SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
return cn;
}
}
}
Clase Contratista
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace pjMantenimientoContratista
{
public class Contratista
{
public string codigo { get; set; }
public string nombre { get; set; }
public string paterno { get; set; }
public string materno { get; set; }
public string telefono { get; set; }
public string correo { get; set; }
}
}
Página 9 de 17
13.- Agregar la clase LogicaNegocio y colocar el siguiente Código:
Clase LogicaNegocio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace pjMantenimientoContratista
{
public class LogicaNegocio
{
// Definición global
Conexion objCon = new Conexion();
SqlConnection cn = new SqlConnection();
string mensaje;
Página 10 de 17
catch(SqlException ex)
{
mensaje = ex.Message;
}
finally
{
cn.Close();
}
return mensaje;
}
try
{
int n = cmd.ExecuteNonQuery();
mensaje = n.ToString() + " CONTRATISTA ACTUALIZADO CORRECTAMENTE";
}
catch (SqlException ex)
{
mensaje = ex.Message;
}
finally
{
cn.Close();
}
return mensaje;
}
Página 11 de 17
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("COD", SqlDbType.VarChar).Value = objC.codigo;
try
{
int n = cmd.ExecuteNonQuery();
mensaje = n.ToString() + " CONTRATISTA ELIMINADO CORRECTAMENTE";
}
catch (SqlException ex)
{
mensaje = ex.Message;
}
finally
{
cn.Close();
}
return mensaje;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace pjMantenimientoContratista
{
public partial class frmNuevoContratista : Form
{
LogicaNegocio objL = new LogicaNegocio();
public frmNuevoContratista()
{
InitializeComponent();
}
void generaCodigo()
{
lblCodigo.Text = objL.generaCodigo();
}
Página 12 de 17
objCo.materno = txtMaterno.Text;
objCo.telefono = txtFono.Text;
objCo.correo = txtCorreo.Text;
void limpiarControles()
{
generaCodigo();
txtNombre.Clear();
txtPaterno.Clear();
txtMaterno.Clear();
txtFono.Clear();
txtCorreo.Clear();
txtNombre.Focus();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace pjMantenimientoContratista
{
public partial class frmActualizaContratista : Form
{
LogicaNegocio objL = new LogicaNegocio();
public frmActualizaContratista()
{
InitializeComponent();
}
Página 13 de 17
private void btnBuscar_Click(object sender, EventArgs e)
{
Contratista objCo = new Contratista();
objCo.codigo = txtCodigo.Text;
DataRow fila = objL.buscaContratista(objCo).Rows[0];
txtNombre.Text=fila[1].ToString();
txtPaterno.Text=fila[2].ToString();
txtMaterno.Text=fila[3].ToString();
txtFono.Text=fila[4].ToString();
txtCorreo.Text=fila[5].ToString();
}
void limpiarControles()
{
txtCodigo.Clear();
txtNombre.Clear();
txtPaterno.Clear();
txtMaterno.Clear();
txtFono.Clear();
txtCorreo.Clear();
txtCodigo.Focus();
}
}
}
}
Página 14 de 17
16.- Código del formulario: frmEliminaContratista se muestra a continuación.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace pjMantenimientoContratista
{
public partial class frmEliminaContratista : Form
{
LogicaNegocio objL = new LogicaNegocio();
public frmEliminaContratista()
{
InitializeComponent();
}
txtNombre.Text = fila[1].ToString();
txtPaterno.Text = fila[2].ToString();
txtMaterno.Text = fila[3].ToString();
txtFono.Text = fila[4].ToString();
txtCorreo.Text = fila[5].ToString();
}
Página 15 de 17
private void btnSalir_Click(object sender, EventArgs e)
{
this.Close();
}
void limpiarControles()
{
txtCodigo.Clear();
txtNombre.Clear();
txtPaterno.Clear();
txtMaterno.Clear();
txtFono.Clear();
txtCorreo.Clear();
txtCodigo.Focus();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace pjMantenimientoContratista
{
public partial class frmMenuPrincipal : Form
{
public frmMenuPrincipal()
{
InitializeComponent();
}
Página 16 de 17
private void mnuEliminar_Click(object sender, EventArgs e)
{
foreach (Form formularios in MdiChildren)
{
formularios.Close();
}
Página 17 de 17