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

APLICACIONES CON BASE DE

DATOS
EN SQL SERVER
create database BASES
go
use BASES
go
create table cargo(codigo int primary key,descripcion varchar(30))
go
insert into cargo values(1,'Gerente')
insert into cargo values(2,'Secretaria')
insert into cargo values(3,'Abogado')
go
select * from cargo

EN VISUAL BASIC NET


EN MODULO
Imports System.Data.SqlClient
Module Module1
Public cnx As New SqlConnection
Dim f As New Form2
Sub main()
Try
cnx.ConnectionString = "Data Source=.;initial catalog=bases;integrated
security=true"
f.ShowDialog()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Module

EN FORMULARIO FORM1

Imports System.Data.SqlClient
Public Class Form1
Dim da As New SqlDataAdapter("select * from cargo", cnx)
Sub mostrar()
Dim ds As New DataSet
da.Fill(ds, "cargo")
DataGridView1.DataSource = ds.Tables(0)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call mostrar()
End Sub
End Class

EN FORMULARIO FORM2

Imports System.Data.SqlClient
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim sql As String
sql = "insert into cargo values('" + TextBox1.Text + "','" + TextBox2.Text + "')"
If cnx.State = 1 Then cnx.Close()
cnx.Open()
Dim cmd As New SqlCommand(sql, cnx)
cmd.ExecuteNonQuery()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Dim sql As String
Dim dr As SqlDataReader
sql = "select * from cargo where codigo='" + TextBox1.Text + "'"
If cnx.State = 1 Then cnx.Close()
cnx.Open()
Dim cmd As New SqlCommand(sql, cnx)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows = True Then
TextBox2.Text = dr(1)
Else
MsgBox("Codigo no existe")
End If

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Dim sql As String
sql = "update cargo set descripcion='" + TextBox2.Text + "' where codigo='" +
TextBox1.Text + "'"
If cnx.State = 1 Then cnx.Close()
cnx.Open()
Dim cmd As New SqlCommand(sql, cnx)
cmd.ExecuteNonQuery()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Dim sql As String
sql = "delete from cargo where codigo='" + TextBox1.Text + "'"
If cnx.State = 1 Then cnx.Close()
cnx.Open()
Dim cmd As New SqlCommand(sql, cnx)
cmd.ExecuteNonQuery()
End Sub
End Class

You might also like