Sie sind auf Seite 1von 6

SERVICIO Estudiante Insert

Public Function insertar(ByVal es As Estudiante) As Boolean


conectar()
Dim cmd As OleDbCommand
cmd = New OleDbCommand("insert into Estudiante(nombre,apellido,correo)" & "Values('" &
es.GetNombre.ToString & "','" & es.GetApellido.ToString & "','" & es.GetCorreo.ToString & "')", cnn)
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
desconectar()
End Function

Clase formulario Insert

Private Sub btnInsertar_Click(sender As Object, e As EventArgs) Handles btnInsertar.Click


Dim es As New Estudiante
Dim ses As New ServicioEstudiante
Dim ban As Boolean
es.SetNombre(txtNombre.Text)
es.SetApellido(txtApellido.Text)
es.SetCorreo(txtCorreo.Text)
ban = ses.insertar(es)
If ban Then
MsgBox("Insercion Exitosa")
mostrar()
Else
MsgBox("Insercion NO Exitosa")
mostrar()

End If
End Sub

CLASE PERSONA

Public Class Persona


Private Id As Integer
Private nombre As String
Private apellido As String
Private correo As String
Sub New(ByVal Id As Integer, ByVal nombre As String, ByVal apellido As String, ByVal correo As String)
Me.Id = Id
Me.nombre = nombre
Me.apellido = apellido
Me.correo = correo
End Sub
Sub New()
Me.Id = Id
Me.nombre = nombre
Me.apellido = apellido
Me.correo = correo
End Sub
Function GetId() As Integer
Return Me.Id
End Function
Function GetNombre() As String
Return Me.nombre
End Function
Function GetApellido() As String
Return Me.apellido
End Function
Function GetCorreo() As String
Return Me.correo
End Function
Function SetId(ByVal Id As Integer)
Me.Id = Id
Return Nothing
End Function
Function SetNombre(ByVal nombre As String)
Me.nombre = nombre
Return Nothing
End Function
Function SetApellido(ByVal apellido As String)
Me.apellido = apellido
Return Nothing
End Function
Function SetCorreo(ByVal correo As String)
Me.correo = correo
Return Nothing
End Function
End Class

FORMULARIO

Public Class Form1

Private Sub btnGenerar_Click(sender As Object, e As EventArgs) Handles btnGenerar.Click


Dim per As New Persona(1, "Laura", "Gutierrez", "laura@gmail.com")
Dim id As Integer
Dim nom, apell, corr As String

id = per.GetId
nom = per.GetNombre
apell = per.GetApellido
corr = per.GetCorreo

MessageBox.Show("Los datos almacenados son:" & id & " " & nom & " " & apell & " " & corr)

End Sub

Private Sub btnCambiar_Click(sender As Object, e As EventArgs) Handles btnCambiar.Click


Dim per As New Persona
Dim id As Integer
Dim nom, apell, corr As String

per.SetId(CInt(txtcodigo.Text))
per.SetNombre(txtNombre.Text)
per.SetApellido(txtApellido.Text)
per.SetCorreo(txtCorreo.Text)

id = per.GetId
nom = per.GetNombre
apell = per.GetApellido
corr = per.GetCorreo

MessageBox.Show("Los datos almacenados son:" & id & " " & nom & " " & apell & " " & corr)

End Sub

Private Sub btnIngresar_Click(sender As Object, e As EventArgs) Handles btnIngresar.Click


Dim per As New Persona
Dim id As Integer
Dim nom, apell, corr As String

per.SetId(8)
per.SetNombre("Tania")
per.SetApellido("Rodriguez")
per.SetCorreo("tania@gmail.com")
id = per.GetId
nom = per.GetNombre
apell = per.GetApellido
corr = per.GetCorreo

MessageBox.Show("Los datos almacenados son:" & id & " " & nom & " " & apell & " " & corr)

End Sub
End Class

Servicio Estudiante Delete

Public Function eliminar(ByVal es As Estudiante) As Boolean


conectar()
Dim cmd As OleDbCommand
cmd = New OleDbCommand("Delete From Estudiante where Id=" & CInt(es.GetId.ToString) & "", cnn)
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
desconectar()
End Function
Clase Formulario

Private Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click


Dim es As New Estudiante
Dim sest As New Servicio
Dim res As DialogResult

res = MessageBox.Show("¿Realmente desea Eliminar al estudiante seleccionado?", "Modificando registro",


MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
If res = Windows.Forms.DialogResult.OK Then
For Each row As DataGridViewRow In DataListado.Rows
Dim marcado As Boolean
marcado = Convert.ToBoolean(row.Cells("Eliminar").Value)
If marcado Then
Dim Idcodigo As Integer
Idcodigo = Convert.ToInt32(row.Cells("Id").Value)
es.SetId(Idcodigo)
If sest.eliminar(es) Then
mostrar()

Else
MessageBox.Show("Error")
End If
End If

Next
mostrar()
Else
MessageBox.Show("Cancelado")
mostrar()

End If

End Sub

Private Sub DataListado_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles


DataListado.CellContentClick
If e.ColumnIndex = Me.DataListado.Columns.Item("Eliminar").Index Then
Dim chk As DataGridViewCheckBoxCell = DataListado.Rows(e.RowIndex).Cells("Eliminar")
chk.Value = Not chk.Value
End If
End Sub

SERVICIO ESTUDIANTE UPDATE

Public Function Editar(ByVal es As ESTUDIANTE) As Boolean


conectar()
Dim cmd As OleDbCommand
cmd = New OleDbCommand("Update Estudiante set NOMBRE='" & es.GetNombre.ToString & "',APELLIDO='" &
es.GetApellido.ToString & "',CORREO='" & es.GetCorreo.ToString & "' where id=" & CInt(es.GetId.ToString) & "", cnn)

If cmd.ExecuteNonQuery Then
Return True
Else
Return False

End If
desconectar()
End Function

CLASE FORMULARIO

Private Sub buscar()


Dim ds As New DataSet
ds.Tables.Add(DT.Copy)
Dim dv As New DataView(ds.Tables(0))
dv.RowFilter = cboCampo.Text & " like '" & txtBuscar.Text & "%'"
If dv.Count <> 0 Then
DataLISTADO.DataSource = dv
ocultar_columna()
Else
DataLISTADO.DataSource = Nothing

End If

End Sub

Private Sub ocultar_columna()


DataLISTADO.Columns(1).Visible = False
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim res As DialogResult
res = MessageBox.Show(",¿Realmente desea Modificar?", "modificando registro", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question)
If res = Windows.Forms.DialogResult.OK Then
Dim es As New ESTUDIANTE
Dim sest As New SERVICIOESTUDIANTE
Dim ban As Boolean
es.SetId(TextBox1.Text)
es.SetNombre(TxtNOMBRE.Text)
es.SetApellido(TxtApellido.Text)
es.SetCorreo(TxtCORREO.Text)
ban = sest.Editar(es)
If ban Then
MsgBox("Insercion exitosa")
MOSTRAR()
Else
MsgBox("Insercion no exitosa")
MOSTRAR()
End If
End If
End Sub

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


TextBox1.Text = DataLISTADO.SelectedCells(1).Value
TxtNOMBRE.Text = DataLISTADO.SelectedCells(2).Value
TxtApellido.Text = DataLISTADO.SelectedCells(3).Value
TxtCORREO.Text = DataLISTADO.SelectedCells(4).Value
End Sub

CLASE CONECCION

Imports System.Data.OleDb
Public Class coneccion
Protected cnn As OleDb.OleDbConnection
Dim coneccionstring As String
Protected Function conectar()
coneccionstring = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & "academico.accdb" & ";" & "Persist
Security Info=False"
cnn = New OleDbConnection(coneccionstring)
cnn.Open()
Return True
End Function
Protected Function desconectar()
If cnn.State.Equals(ConnectionState.Open) Then
cnn.Close()
Return True
Else
Return False
End If
End Function
End Class

CLASE SERVICIOESTUDIANTE SELECT

Imports System.Data.OleDb
Public Class ServicioEstudiante
Inherits coneccion

Public Function mostrar_tabla()


conectar()
Dim dt As New DataTable
Dim da As New OleDbDataAdapter("Select * From Estudiante", cnn)
da.Fill(dt)
If dt.Rows.Count <> 0 Then
Return dt
Else
Return Nothing
desconectar()
End If

End Function
End Class

CLASE ESTUDIANTE

Public Class Estudiante


Private Id As Integer
Private nombre As String
Private apellido As String
Private correo As String
Sub New(ByVal Id As Integer, ByVal nombre As String, ByVal apellido As String, ByVal correo As String)
Me.Id = Id
Me.nombre = nombre
Me.apellido = apellido
Me.correo = correo
End Sub
Sub New()
Me.Id = Id
Me.nombre = nombre
Me.apellido = apellido
Me.correo = correo
End Sub
Function GetId() As Integer
Return Me.Id
End Function
Function GetNombre() As String
Return Me.nombre
End Function
Function GetApellido() As String
Return Me.apellido
End Function
Function GetCorreo() As String
Return Me.correo
End Function
Function SetId(ByVal Id As Integer)
Me.Id = Id
Return Nothing
End Function
Function SetNombre(ByVal nombre As String)
Me.nombre = nombre
Return Nothing
End Function
Function SetApellido(ByVal apellido As String)
Me.apellido = apellido
Return Nothing
End Function
Function SetCorreo(ByVal correo As String)
Me.correo = correo
Return Nothing
End Function

End Class
CLASE FORMULARIO

Public Class Form1

Private dt As New DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


mostrar()
End Sub

Private Sub mostrar()


Dim sest As New ServicioEstudiante
dt = sest.mostrar_tabla
If dt.Rows.Count <> 0 Then
DataListado.DataSource = dt
DataListado.ColumnHeadersVisible = True
Else
DataListado.DataSource = Nothing
DataListado.ColumnHeadersVisible = False
End If
End Sub

End Class

Das könnte Ihnen auch gefallen