Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

INTERFACES

CÓDIGOS

Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim conexion As SqlConnection
conexion = New SqlConnection("server=LAPTOP-D2U5FLAU;database=Tienda;integrated security =
true")
conexion.Open()
MessageBox.Show("Se abrió la conexión con el servidor SQL Server y se seleccionó la base de datos")
conexion.Close()
MessageBox.Show("Se cerró la conexión.")

End Sub
End Class
Imports System.Data.SqlClient
Public Class Producto
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Btngrabar.Click
Dim conexion As SqlConnection
conexion = New SqlConnection("server=LAPTOP-D2U5FLAU ; database=Tienda ; integrated security =
true")
conexion.Open()
Dim Idprod As String = Txtprod.Text
Dim Nomb As String = Txtnomb.Text
Dim Prec As String = Txtprec.Text
Dim Idcat As String = Txtcat.Text
Dim cadena As String = "insert into articulos(Idcat,descrip) values ('" & Idprod & "'," & Nomb & "," &
Prec & "," & Idcat & " )"
Dim comando As SqlCommand
comando = New SqlCommand(cadena, conexion)
comando.ExecuteNonQuery()
MessageBox.Show("Los datos se guardaron correctamente")
Txtprod.Text = ""
Txtnomb.Text = ""
Txtprec.Text = ""
Txtcat.Text = ""
conexion.Close()
End Sub

Private Sub Btnbuscar_Click(sender As Object, e As EventArgs) Handles Btnbuscar.Click


Dim conexion As SqlConnection
conexion = New SqlConnection("server=LAPTOP-D2U5FLAU ; database=Tienda; integrated security =
true")
conexion.Open()
Dim cod As String = Txtcodbus.Text
Dim cadena As String = "select descripcion, precio from articulos where codigo=" & cod
Dim comando As SqlCommand
comando = New SqlCommand(cadena, conexion)
Dim registro As SqlDataReader
registro = comando.ExecuteReader()

If registro.Read() Then
Lblidprod.Text = registro("Idproducto")
Lblnomb.Text = registro("Nombre")
Lblprec.Text = registro("Precio")
Lblidcat.Text = registro("Idcategoria")
Btnelimi.Enabled = True
Else
MessageBox.Show("No existe un artículo con el código ingresado")
End If
conexion.Close()
End Sub

Private Sub Btnelimi_Click(sender As Object, e As EventArgs) Handles Btnelimi.Click


Dim conexion As SqlConnection
Dim cod As String = Txtcodbus.Text
Dim cadena As String = "delete from articulos where codigo=" & cod
Dim comando As SqlCommand
comando = New SqlCommand(cadena, conexion)
Dim pre As Integer
pre = comando.ExecuteNonQuery()
If pre = 1 Then
Lblidprod.Text = ""
Lblnomb.Text = ""
Lblprec.Text = ""
Lblidcat.Text = ""
MessageBox.Show("Se borró el artículo")
Else
MessageBox.Show("No existe un artículo con el código ingresado")
End If
conexion.Close()
Btnelimi.Enabled = False
End Sub

Private Sub Btnnuevo_Click(sender As Object, e As EventArgs) Handles Btnnuevo.Click


Txtprod.Text = ""
Txtnomb.Text = ""
Txtprec.Text = ""
Txtcat.Text = ""
End Sub
End Class

Imports System.Data.SqlClient
Public Class Categoria
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Btngrabar.Click
Dim conexion As SqlConnection
conexion = New SqlConnection("server=LAPTOP-D2U5FLAU ; database=Tienda ; integrated security =
true")
conexion.Open()
Dim Idcat As String = Txtcat.Text
Dim descrip As String = Txtdescrip.Text
Dim cadena As String = "insert into articulos(Idcat,descrip) values ('" & Idcat & "'," & descrip & ")"
Dim comando As SqlCommand
comando = New SqlCommand(cadena, conexion)
comando.ExecuteNonQuery()
MessageBox.Show("Los datos se guardaron correctamente")
Txtcat.Text = ""
Txtdescrip.Text = ""
conexion.Close()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Btnbuscar.Click


Dim conexion As SqlConnection
conexion = New SqlConnection("server=LAPTOP-D2U5FLAU ; database=Tienda; integrated security =
true")
conexion.Open()
Dim cod As String = Txtcodbus.Text
Dim cadena As String = "select descripcion, precio from articulos where codigo=" & cod
Dim comando As SqlCommand
comando = New SqlCommand(cadena, conexion)
Dim registro As SqlDataReader
registro = comando.ExecuteReader()

If registro.Read() Then
Lblidcat.Text = registro("Idcategoria")
Lbldescrip.Text = registro("Descripcion")
btnelimi.Enabled = True
Else
MessageBox.Show("No existe un artículo con el código ingresado")
End If
conexion.Close()
End Sub

Private Sub Btnnuevo_Click(sender As Object, e As EventArgs) Handles Btnnuevo.Click


Txtcodbus.Text = ""
Txtcat.Text = ""
Txtcodbus.Text = ""
End Sub

Private Sub Eliminar_Click(sender As Object, e As EventArgs) Handles btnelimi.Click


Dim conexion As SqlConnection
Dim cod As String = Txtcodbus.Text
Dim cadena As String = "delete from articulos where codigo=" & cod
Dim comando As SqlCommand
comando = New SqlCommand(cadena, conexion)
Dim pre As Integer
pre = comando.ExecuteNonQuery()
If pre = 1 Then
Lblidcat.Text = ""
Lbldescrip.Text = ""
MessageBox.Show("Se borró el artículo")
Else
MessageBox.Show("No existe un artículo con el código ingresado")
End If
conexion.Close()
btnelimi.Enabled = False
End Sub
End Class

You might also like