Caso en Visual Studio Practica2 haciendo mantenimientos a una tabla
En esta ocasión realizaremos un caso en VBnet en el cual crearemos una base de datos y una tabla donde desde un formulario de Windows Forms realice los mantenimientos respectivos.
Crear la base de datos PRACTICA2 con la tabla CLIENTES con los campos (código, nombres, apellidos, sexo), ingresar 10 clientes, programar un formulario realizar el mantenimiento de la tabla clientes.
Creacion de la BD y la tabla Clientes USE MASTER CREATE DATABASE PRACTICA2 USE PRACTICA2 CREATE TABLE CLIENTES ( codigo varchar(50) NOT NULL primary key, nombres varchar(50) NULL, apellidos varchar(50) NULL, sexo varchar(50) NULL )
insert into CLIENTES values ('CLI001','NOELIA','GARCIA','FEMENINO'); insert into CLIENTES values ('CLI002','FELIX','RICART','MASCULINO'); insert into CLIENTES values ('CLI003','MARIA','OLMEDO','FEMENINO'); insert into CLIENTES values ('CLI004','RAQUEL','GALAN','FEMENINO'); insert into CLIENTES values ('CLI005','AURORA','SANTOS','FEMENINO'); insert into CLIENTES values ('CLI006','RAFAEL','DOMENECH','MASCULINO'); insert into CLIENTES values ('CLI007','RICARDO','GARCIA','MASCULINO'); insert into CLIENTES values ('CLI008','LUIS','IRANZO','MASCULINO'); insert into CLIENTES values ('CLI009','MARIA','RUA','FEMENINO'); insert into CLIENTES values ('CLI010','SUSANA','LANDA','FEMENINO');
Imports System.Data.SqlClient Public Class Form1 Public cn As SqlConnection Public comando As SqlCommand Public data As SqlDataReader Private Sub BTNCancel_Click(sender As Object, e As EventArgs) Handles BTNCancel.Click Close() End Sub Private Sub BTNNuevo_Click(sender As Object, e As EventArgs) Handles BTNNuevo.Click limpiar() End Sub Private Sub limpiar() TXTApellidos.Text = "" TXTCodigo.Text = "" TXTNombres.Text = "" TXTSexo.Text = "" End Sub Private Sub BTNBuscar_Click(sender As Object, e As EventArgs) Handles BTNBuscar.Click Try cn.Open() comando = cn.CreateCommand() comando.CommandText = "select *from CLIENTES where codigo='" + TXTCodigo.Text + "'" data = comando.ExecuteReader() If (data.Read) Then TXTCodigo.Text = data(0) TXTNombres.Text = data(1) TXTApellidos.Text = data(2) TXTSexo.Text = data(3) End If cn.Close() Catch ex As Exception End Try End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load cn = New SqlConnection("Data Source=.;Initial Catalog=PRACTICA2;Integrated Security=True") End Sub Private Sub BTNGuardar_Click(sender As Object, e As EventArgs) Handles BTNGuardar.Click Try cn.Open() comando = cn.CreateCommand() comando.CommandText = "insert into CLIENTES values ('" + TXTCodigo.Text + "','" + TXTNombres.Text + "','" + TXTApellidos.Text + "','" + TXTSexo.Text + "')" comando.ExecuteNonQuery() MsgBox("Registro ingresado correctamente") cn.Close() limpiar() Catch ex As Exception End Try End Sub Private Sub BTNEliminar_Click(sender As Object, e As EventArgs) Handles BTNEliminar.Click Try cn.Open() comando = cn.CreateCommand() comando.CommandText = "delete from CLIENTES where codigo='" + TXTCodigo.Text + "'" comando.ExecuteNonQuery() MsgBox("Registro eliminado correctamente") cn.Close() limpiar() Catch ex As Exception End Try End Sub Private Sub BTNEditar_Click(sender As Object, e As EventArgs) Handles BTNEditar.Click Try cn.Open() comando = cn.CreateCommand() comando.CommandText = "update CLIENTES SET nombres='" + TXTNombres.Text + "',apellidos='" + TXTApellidos.Text + "',sexo='" + TXTSexo.Text + "' where codigo='" + TXTCodigo.Text + "'" comando.ExecuteNonQuery() MsgBox("Registro actualizado correctamente") cn.Close() limpiar() Catch ex As Exception End Try End Sub End Class
Publicar un comentario
0 Comentarios