You are on page 1of 8

1 sola tabla (PAIS)

ALTER PROC [dbo].[USP_ACTUALIZAR_PAIS](@NOM VARCHAR(35),@SUPER INT)


AS
UPDATE PAIS SET SUPERFICIE=@SUPER
WHERE NOMBRE=@NOM

ALTER PROC [dbo].[USP_BORRAR_PAISxNOMBRE](@NOMBRE AS VARCHAR(35))


AS
DELETE FROM PAIS
WHERE NOMBRE=@NOMBRE;

ALTER PROC [dbo].[USP_INSERTAR_PAIS](@NOM VARCHAR(35), @SUPER INT)


AS
INSERT INTO PAIS VALUES (@NOM,@SUPER)

ALTER PROC [dbo].[USP_MOSTRARPAISES]


AS
SELECT * FROM PAIS

ALTER PROC [dbo].[USP_PAIS_ELIMINAR_ALL]


AS
DELETE FROM PAIS

ALTER PROC [dbo].[USP_PAISxNOMBRE](@NOM VARCHAR(35))


AS
SELECT NOMBRE,SUPERFICIE FROM PAIS
WHERE NOMBRE=@NOM

ALTER PROC [dbo].[USP_VALIDAR](@NOM VARCHAR(35))


AS
IF EXISTS (SELECT * FROM PAIS WHERE NOMBRE=@NOM)
BEGIN
SELECT 1
END
ELSE
BEGIN
SELECT 0
END

Imports System.Data.SqlClient
Imports System.Configuration
Public Class Operaciones

Dim cn As New
SqlConnection(ConfigurationManager.ConnectionStrings("conexion").ConnectionString
)

Public Function Sentinel(nombre As String) As DataTable


Try
Dim da As New SqlDataAdapter("USP_VALIDAR", cn)
With da.SelectCommand
.CommandType = CommandType.StoredProcedure
.Parameters.AddWithValue("@NOM", nombre)
End With
Dim dt As New DataTable
da.Fill(dt)
Return dt
Catch ex As Exception
Throw ex
End Try
End Function

Public Sub Actualizar(nombre As String, super As Integer)


Try
Dim cmd As New SqlCommand("USP_ACTUALIZAR_PAIS", cn)
cmd.CommandType = CommandType.StoredProcedure
With cmd.Parameters
.AddWithValue("@NOM", nombre)
.AddWithValue("@SUPER", super)
End With
cn.Open() : cmd.ExecuteNonQuery() : cn.Close()
Catch ex As Exception
Throw ex
End Try
End Sub

Public Sub guardarPais(nombre As String, super As Integer)


Try
Dim cmd As New SqlCommand("USP_INSERTAR_PAIS", cn)
cmd.CommandType = CommandType.StoredProcedure
With cmd.Parameters
.AddWithValue("@NOM", nombre)
.AddWithValue("@SUPER", super)
End With
cn.Open() : cmd.ExecuteNonQuery() : cn.Close()
Catch ex As Exception
Throw ex
End Try
End Sub

Public Function TraerPais(nom As String) As DataTable


Try
Dim da As New SqlDataAdapter("USP_PAISxNOMBRE", cn)
With da.SelectCommand
.CommandType = CommandType.StoredProcedure
.Parameters.AddWithValue("@NOM", nom)
End With
Dim dt As New DataTable
da.Fill(dt)
Return dt
Catch ex As Exception
Throw ex
End Try
End Function

Public Sub BorrarTodosPaises()


Try
Dim cmd As New SqlCommand("USP_PAIS_ELIMINAR_ALL", cn)
cmd.CommandType = CommandType.StoredProcedure
cn.Open() : cmd.ExecuteNonQuery() : cn.Close()
Catch ex As Exception
Throw ex
End Try
End Sub

Public Function Mostrar_Paises() As DataTable


Try
Dim da As New SqlDataAdapter("USP_MOSTRARPAISES", cn)
Dim dt As New DataTable
da.Fill(dt)
Return dt
Catch ex As Exception
Throw ex
End Try
End Function

Public Sub borrarPaisXNombre(nombre As String)


Try
Dim cmd As New SqlCommand("USP_BORRAR_PAISxNOMBRE", cn)
cmd.CommandType = CommandType.StoredProcedure
With cmd.Parameters
.AddWithValue("@NOMBRE", nombre)
End With
cn.Open() : cmd.ExecuteNonQuery() : cn.Close()
Catch ex As Exception
Throw ex
End Try
End Sub

End Class

Public Class GestionPAIS

Dim obj As New Operaciones

Private Sub GestionPAIS_Load(sender As Object, e As EventArgs) Handles


MyBase.Load

End Sub

Private Sub BtnLimpiar_Click(sender As Object, e As EventArgs) Handles


btnLimpiar.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub

Private Sub BtnGuardar_Click(sender As Object, e As EventArgs) Handles


btnGuardar.Click

If (TextBox1.Text = "" Or TextBox2.Text = "") Then


MessageBox.Show("LLENAR CASILLEROS")
Else
Dim dt As New DataTable
dt = obj.Sentinel(TextBox1.Text)
Label3.Text = dt.Rows(0)(0)
If (Integer.Parse(Label3.Text) = 0) Then
obj.guardarPais(TextBox1.Text, Integer.Parse(TextBox2.Text))
MessageBox.Show("Registro exitoso")
Else
MessageBox.Show("Ya existe este registro")
End If
End If
End Sub

Private Sub BtnBuscar_Click(sender As Object, e As EventArgs) Handles


btnBuscar.Click
Dim nombre As String
nombre = InputBox("Ingrese nombre del Pais")
Dim dt As New DataTable
dt = obj.Sentinel(nombre)
Label3.Text = dt.Rows(0)(0)
If (Integer.Parse(Label3.Text) = 0) Then
MessageBox.Show("No existe este registro")
Else
dt = obj.TraerPais(nombre)
TextBox1.Text = dt.Rows(0)(0)
TextBox2.Text = dt.Rows(0)(1)
End If
End Sub

Private Sub BtnEliminarAll_Click(sender As Object, e As EventArgs) Handles


btnEliminarAll.Click
obj.BorrarTodosPaises()
MessageBox.Show("Paises eliminados indiscriminadamente")
End Sub

Private Sub BtnMostrar_Click(sender As Object, e As EventArgs) Handles


btnMostrar.Click
DataGridView1.DataSource = obj.Mostrar_Paises()
End Sub

Private Sub BtnEliminar1_Click(sender As Object, e As EventArgs) Handles


btnEliminar1.Click
Dim nombre As String
nombre = InputBox("Ingrese nombre del pais que quiere borrar")
Dim dt As New DataTable
dt = obj.Sentinel(nombre)
Label3.Text = dt.Rows(0)(0)
If (Integer.Parse(Label3.Text) = 0) Then
MessageBox.Show("No existe este registro")
Else
obj.borrarPaisXNombre(nombre)
End If
End Sub

Private Sub BtnActualizar_Click(sender As Object, e As EventArgs) Handles


btnActualizar.Click
If (TextBox1.Text = "" Or TextBox2.Text = "") Then
MessageBox.Show("No hay que actualizar")
Else
obj.Actualizar(TextBox1.Text, Integer.Parse(TextBox2.Text))
MessageBox.Show("Actualizacion exitosa")
End If
End Sub
End Class

2 tablas
CREATE DATABASE DB
USE DB

CREATE TABLE TABLE1(


FIELD1 VARCHAR(20) PRIMARY KEY,
FIELD2 INT NULL)

CREATE TABLE TABLE2(


FIELD1 VARCHAR(20) PRIMARY KEY,
FIELD2 INT NULL,
FIELD3 VARCHAR(20) NOT NULL REFERENCES TABLE1)

ALTER PROC USP_TABLE1xFIELD1(@FIELD1 VARCHAR(20))


AS
SELECT FIELD1,FIELD2 FROM TABLE1
WHERE FIELD1=@FIELD1

ALTER PROC USP_TABLE1_MOSTRAR_ALL


AS
SELECT * FROM TABLE1

ALTER PROC USP_BORRAR_TABLE1xTABLE2(@CAMPO VARCHAR(20))


AS
IF(NOT EXISTS (SELECT * FROM TABLE2 WHERE TABLE2.FIELD3=@CAMPO))
BEGIN
DELETE FROM TABLE1
WHERE TABLE1.FIELD1=@CAMPO;
SELECT 1
END
ELSE
BEGIN
SELECT 0
END