You are on page 1of 3

1

Instituto Superior Tecnológico Almirante Miguel Grau Piura xcon.Provider = "sqloledb"


Unidad de Formación III xcon.ConnectionString = "userid=" & us & ";password=" & pw & _
Seminario II ";datasource=" & su & ";initial catalog= " & db
Programación de Base de Datos usando Clases xcon.Open
abrir = True
Aplicación que permite comprender en forma práctica el modelo cliente servidor, utilizando Exit Function
módulos de clase en Visual Basic.
errores:
1. Procedimientos que deben ser ejecutados desde el analizador de consultas de abrir = False
SQL server. End Function

create database dbproductos Public Sub cerrar()


go xcon.Close
use dbproductos End Sub
go
create table productos Clase: Lproductos
(codpro int primary key,
nompro varchar (35), Dim rs1 As New Recordset
precio int, Dim cmd As New Command
stock int)
go Public Function existeReg() As Boolean
--procedimientos If rs1.RecordCount <= 0 Then
create proc sp_insproductos existeReg = False
@codpro int,@nompro varchar(35),@precio int,@stock int Else
as existeReg = True
begin End If
insert into productos values(@codpro,@nompro,@precio,@stock) End Function
end
go Public Function cargar()
--probando adicion de productos rs1.Open "select * from productos", xcon, adOpenStatic
execute sp_insproductos 1,'te',2,10 End Function
execute sp_insproductos 2,'cafe',3,15
execute sp_insproductos 3,'algodon',5,20 Public Function descargar()
execute sp_insproductos 4,'arroz',2,40 If rs1.State = adStateOpen Then rs1.Close
execute sp_insproductos 5,'azucar',2,50 End Function
select * from productos
Public Function refresh()
rs1.Close
2. Programación de base de datos utilizando clases con conexión a SQL Server rs1.Open "select * from productos", xcon, adOpenStatic
End Function
Módulos de clases
LConnetion Public Function buscaproducto(c As Integer) As Recordset
Dim rs As New Recordset If rs1.State = 1 Then rs1.Close
Dim rs1 As New Recordset rs1.Open "select * from productos where codpro=" & c, xcon, adOpenStatic
Dim cmd As New Command Set buscaproducto = rs1
End Function
Public Function abrir(sv As String, db As String, us As String, pw As String) As
Boolean
On Error GoTo errores 'prcedimiento para los campos

Ing. CIP Mario Seminario Atarama MSc.


2

If var.abrir("(local)", "dbproductos", "sa", "") Then


Public Function xcodigo() Form1.Show
xcodigo = rs1.Fields(0) Else
End Function MsgBox "Error en el ingreso", vbOKOnly, "Sistema de Productos"
End
Public Function xnompro() End If
xnompro = rs1.Fields(1) End Sub
End Function
Módulo2
Public Function xprecio()
xprecio = rs1.Fields(2) Public xcon As New Connection
End Function Public r As New Recordset
Public Sub conectar()
Public Function xstock() xcon.ConnectionString = "Provider = SQLOLEDB;Password=;Persist Security Info =
xstock = rs1.Fields(3) True;User ID = sa;Initial Catalog=dbproductos;Data Source=(local)"
End Function xcon.Open
End Sub

Public Sub ins_prod(cod As Integer, nom As String, pre As Integer, stoc As Integer) 3. Diseño del formulario
cmd.ActiveConnection = xcon
cmd.CommandText = "sp_insproductos " & cod & "," & nom & ", " & pre & ", " & stoc & ""
cmd.Execute
End Sub

Public Function inicio()


rs1.MoveFirst
End Function

Public Function ultimo()


rs1.MoveLast
End Function

Public Function anterior()


rs1.MovePrevious
If rs1.BOF Then rs1.MoveFirst
End Function
4. Programación en el formulario
Public Function siguiente()
rs1.MoveNext General Declaraciones
If rs1.EOF Then rs1.MoveLast Dim var As New lproductos
End Function

Módulos estándar

Módulo1
Public var As New lconnection Public Sub mostrardatos()
Public prod As New lproductos If Not existeReg Then
Public cod As Integer Text1 = var.xcodigo
Sub main1() If Not IsNull(var.xnompro) Then Text2 = var.xnompro

Ing. CIP Mario Seminario Atarama MSc.


3

If Not IsNull(var.xprecio) Then Text3 = var.xprecio End If


If Not IsNull(var.xstock) Then Text4 = var.xstock End Sub
End If
End Sub Private Sub Form_Load()
On Error GoTo errores
Private Sub cmddesplaza_Click(Index As Integer) Call conectar
Select Case Index var.cargar
Case 0 Call mostrardatos
var.inicio Exit Sub
Case 1
var.anterior errores:
Case 2 If Err.Number = 2147467259 Then
var.siguiente MsgBox (Err.Description)
Case 3 Exit Sub
var.ultimo End If
End Select End Sub
Call mostrardatos
End Sub

Private Sub cmdnuevo_Click()


Dim ctrl As Control
For Each ctrl In Controls
If TypeOf ctrl Is TextBox Then ctrl.Text = ""
Next
Text2.SetFocus
Text1.Enabled = False
End Sub

Private Sub cnmdgrabar_Click()


If Trim(Text2) = "" Then
MsgBox ("debe ingresar su nombre")
Text2.SetFocus
Exit Sub
End If
If Val(Text3) <= 0 Then
MsgBox ("debe ingresar precio")
Text3.SetFocus
Exit Sub
End If
If Val(Text4) <= 0 Then
MsgBox ("debe ingresar stock")
Text4.SetFocus
Exit Sub
End If
If MsgBox("desea grabar:", vbYesNo) = vbYes Then
var.ultimo
Text1 = var.xcodigo + 1
var.ins_prod Text1, Text2, Text3, Text4
var.refresh

Ing. CIP Mario Seminario Atarama MSc.

You might also like