Anda di halaman 1dari 17

REGISTRO DE DOCENTES Y MATERIAS

CLASE CONEXIN

'importar libreria mysql


Imports MySql.Data.MySqlClient
Public Class Conexion

Public registro As MySqlDataReader


Public cmd As New MySqlCommand 'Representa una instruccin SQL o un procedimiento
almacenado para ejecutar con MySQL.
Public da As New MySqlDataAdapter 'representa un conjunto de comandos de datos y
una conexin de base de datos que se utilizan para rellenar un conjunto de datos y
actualizar una base de datos MySQL
Public dt As New DataTable 'representa una tabla de datos relacionales de la memoria;
los datos son locales de la
'aplicacin basada en .NET en la que residen, pero se pueden llenar desde un origen de
datos como Microsoft SQL
'Server mediante un DataAdapter
Public result As String

Public Function strconexion() As MySqlConnection


Return New
MySqlConnection("server=localhost;userid=root;password=;database=registro de
docente")
'parametros de conexion a la base de datos
End Function
Public strcon As MySqlConnection = strconexion()
End Class

CLASE DOCENTE
Public Class docente
Inherits Conexion
Private
Private
Private
Private
Private
Private

codigoDocente As String
nombreDocente As String
apellidoDocente As String
direccionDocente As String
telefonoDocente As String
matriaDocente As String

'metodos de propiedad
Public Property codDocente() As String
Get
Return codigoDocente
End Get
Set(value As String)

codigoDocente = value
End Set
End Property
Public Property nomDocente() As String
Get
Return nombreDocente
End Get
Set(value As String)
nombreDocente = value
End Set
End Property
Public Property apeDocente() As String
Get
Return apellidoDocente
End Get
Set(value As String)
apellidoDocente = value
End Set
End Property
Public Property diDocente() As String
Get
Return direccionDocente
End Get
Set(value As String)
direccionDocente = value
End Set
End Property
Public Property teDocente() As String
Get
Return telefonoDocente
End Get
Set(value As String)
telefonoDocente = value
End Set
End Property
Public Property maDocente() As String
Get
Return matriaDocente
End Get
Set(value As String)
strcon.Open()
With cmd
.Connection = strcon
''deje alumno porque asi lo habia configuardo en mysql se puede cambiar
.CommandText = "SELECT codMateria FROM materia WHERE nomMateria = '" &
value & "'"
End With
registro = cmd.ExecuteReader()
If registro.Read() = True Then
matriaDocente = registro("codMateria")
End If
strcon.Close()

End Set
End Property
'metodo para guardar registros en la tabla docente
Public Sub guardar()
Try
strcon.Open()
With cmd
.Connection = strcon
''deje alumno porque asi lo habia configuardo en mysql se puede cambiar
.CommandText = "INSERT INTO docentes(nomDocente, apeDocente,
direccionDocente, TelefonoDocente, codMatriaFk) values('" & nomDocente & "','" &
apeDocente & "', '" & diDocente & "', '" & teDocente & "', '" & maDocente & "' )"
'ejecutar los datos
result = cmd.ExecuteNonQuery
'chequear si los datos han sido guardados
If result = 0 Then
MsgBox("Ha ocurrido un error al guardar", MsgBoxStyle.Information)
Else
MsgBox("Los datos han sido guardados")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
strcon.Close()
End Sub
Public Sub listarDocente(ByVal DTG As Object, ByVal TXT As TextBox, ByVal CMB As
ComboBox)
Try
dt = New DataTable
strcon.Open()
With cmd
.Connection = strcon
Select Case CMB.Text
Case "Nombre"
.CommandText = "Select codDocente, nomDocente, apeDocente,
direccionDocente, telefonoDocente, materia.nomMateria FROM docentes JOIN materia ON
docentes.codMatriaFk = materia.codMateria WHERE nomDocente like '" & TXT.Text & "%'"
Case "Codigo"
.CommandText = "Select codDocente, nomDocente, apeDocente,
direccionDocente, telefonoDocente, materia.nomMateria FROM docentes JOIN materia ON
docentes.codMatriaFk = materia.codMateria WHERE codDocente like '" & TXT.Text & "%'"
Case "Apellido"
.CommandText = "Select codDocente, nomDocente, apeDocente,
direccionDocente, telefonoDocente, materia.nomMateria FROM docentes JOIN materia ON
docentes.codMatriaFk = materia.codMateria WHERE apeDocente like '" & TXT.Text & "%'"
End Select

If TXT.Text = "" Then

.CommandText = "Select codDocente, nomDocente, apeDocente,


direccionDocente, telefonoDocente, materia.nomMateria FROM docentes JOIN materia ON
docentes.codMatriaFk = materia.codMateria"
End If
End With
da.SelectCommand = cmd
da.Fill(dt)
'DTG.DataSource = dt
Dim btnEditar As DataGridViewButtonColumn = New DataGridViewButtonColumn()
btnEditar.Text = "Editar"
Dim btnEliminar As DataGridViewButtonColumn = New
DataGridViewButtonColumn()
btnEliminar.Text = "Eliminar"
btnEditar.HeaderText = "Editar"
btnEditar.Text = "Editar"
btnEditar.UseColumnTextForButtonValue = True
btnEliminar.HeaderText = "Eliminar"
btnEliminar.Text = "Eliminar"
btnEliminar.UseColumnTextForButtonValue = True
DTG.Rows.Clear()
DTG.Columns.Clear()
DTG.columns.add("codDocente", "Codigo")
DTG.columns.add("nomDocente", "Nombre")
DTG.columns.add("apeDocente", "Apellido")
DTG.columns.add("direccionDocente", "Direccion")
DTG.columns.add("telefonoDocente", "telefono")
DTG.columns.add("nomMateria", "nomMateria")
DTG.columns.add(btnEditar)
DTG.columns.add(btnEliminar)
DTG.Columns(0).Width = 30
DTG.Columns(1).Width = 125
DTG.Columns(2).Width = 125
DTG.Columns(3).Width = 150
DTG.Columns(4).Width = 75
DTG.Columns(5).Width = 75
DTG.Columns(6).Width = 50
DTG.Columns(7).Width = 50
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
DTG.rows.add()
DTG.rows(i).cells("codDocente").value = dt.Rows(i).Item("codDocente")
DTG.Rows(i).cells("nomDocente").value = dt.Rows(i).Item("nomDocente")
DTG.Rows(i).cells("apeDocente").value = dt.Rows(i).Item("apeDocente")
DTG.Rows(i).cells("direccionDocente").value =
dt.Rows(i).Item("direccionDocente")
DTG.Rows(i).cells("telefonoDocente").value = dt.Rows(i).Item("telefonoDocente")
DTG.Rows(i).cells("nomMateria").value = dt.Rows(i).Item("nomMateria")
Next
Catch ex As Exception
MsgBox(ex.Message)

End Try
strcon.Close()
da.Dispose()
End Sub
Public Sub eliminarDocente()

& "'"

Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = "DELETE FROM docentes WHERE codDocente='" & codDocente

'ejecutar los datos


result = cmd.ExecuteNonQuery
'chequear si los datos han sido eliminados
If result = 0 Then
MsgBox("Ha ocurrido un error al eliminar registro", MsgBoxStyle.Information)
Else
MsgBox("Los datos han sido eliminados")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
strcon.Close()
End Sub
Public Sub actualizarDocente()
Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = "UPDATE docentes SET nomDocente='" & nomDocente &
"',apeDocente='" & apeDocente & "', direccionDocente='" & diDocente & "',
telefonoDocente='" & teDocente & "', codMatriaFk='" & maDocente & "' WHERE
codDocente='" & codDocente & "'"
'ejecutar los datos
result = cmd.ExecuteNonQuery
'chequear si los datos han sido actualizados
If result = 0 Then
MsgBox("Ha ocurrido un error al modificar registro", MsgBoxStyle.Information)
Else
MsgBox("Los datos han sido actualizados")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
strcon.Close()
End Sub
End Class

CLASE MATERIA
Imports MySql.Data.MySqlClient
Public Class materia
Inherits Conexion
'declaracion de propiedades de la clase
Private _codMateria As Integer
Private nombreMateria As String
Private descripcionMateria As String
'metodos de propiedad
Public Property codMateria() As Integer
Get
Return _codMateria
End Get
Set(value As Integer)
_codMateria = value
End Set
End Property
Public Property nomMateria() As String
Get
Return nombreMateria
End Get
Set(value As String)
nombreMateria = value
End Set
End Property
Public Property desMateria() As String
Get
Return descripcionMateria
End Get
Set(value As String)
descripcionMateria = value
End Set
End Property
Public Sub listarMateria(ByVal DTG As Object, ByVal TXT As TextBox, ByVal CMB As
ComboBox)
Try
dt = New DataTable
strcon.Open()
With cmd
.Connection = strcon
Select Case CMB.Text
Case "Codigo"
.CommandText = "SELECT codMateria, nomMateria, desMateria FROM
materia WHERE codMateria like '" & TXT.Text & "%'"
Case "Nombre"
.CommandText = "SELECT codMateria, nomMateria, desMateria FROM
materia WHERE nomMateria like '" & TXT.Text & "%'"
End Select
If TXT.Text = "" Then

.CommandText = "SELECT codMateria, nomMateria, desMateria FROM materia"


End If
End With
da.SelectCommand = cmd
da.Fill(dt)
Dim btnEditar As DataGridViewButtonColumn = New DataGridViewButtonColumn()
btnEditar.Text = "Editar"
Dim btnEliminar As DataGridViewButtonColumn = New
DataGridViewButtonColumn()
btnEliminar.Text = "Eliminar"
btnEditar.HeaderText = "Editar"
btnEditar.Text = "Editar"
btnEditar.UseColumnTextForButtonValue = True
btnEliminar.HeaderText = "Eliminar"
btnEliminar.Text = "Eliminar"
btnEliminar.UseColumnTextForButtonValue = True
DTG.Rows.Clear()
DTG.Columns.Clear()
DTG.columns.add("codMateria", "Codigo")
DTG.columns.add("nomMateria", "Nombre")
DTG.columns.add("desMateria", "Descripcion")
DTG.columns.add(btnEditar)
DTG.columns.add(btnEliminar)

DTG.Columns(0).Width
DTG.Columns(1).Width
DTG.Columns(2).Width
DTG.Columns(3).Width
DTG.Columns(4).Width

=
=
=
=
=

30
100
200
50
50

Dim i As Integer
For i = 0 To dt.Rows.Count - 1
DTG.rows.add()
DTG.rows(i).cells("codMateria").value = dt.Rows(i).Item("codMateria")
DTG.Rows(i).cells("nomMateria").value = dt.Rows(i).Item("nomMateria")
DTG.Rows(i).cells("desMateria").value = dt.Rows(i).Item("desMateria")
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
strcon.Close()
da.Dispose()
End Sub
Public Sub guardarMateria()

Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = "INSERT INTO materia(nomMateria, desMateria) values('" &
nomMateria & "','" & desMateria & "' )"
'ejecutar los datos
result = cmd.ExecuteNonQuery
'chequear si los datos han sido guardados
If result = 0 Then
MsgBox("Ha ocurrido un error al guardar", MsgBoxStyle.Information)
Else
MsgBox("La materia se ha registrado Correctamente!")
End If
End With
strcon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub actualizarMateria()
Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = "UPDATE materia SET nomMateria='" & nomMateria &
"',desMateria='" & desMateria & "' WHERE codMateria='" & codMateria & "'"
'ejecutar los datos
result = cmd.ExecuteNonQuery
'chequear si los datos han sido actualizados
If result = 0 Then
MsgBox("Ha ocurrido un error al modificar registro", MsgBoxStyle.Information)
Else
MsgBox("La materia se ha actualizado correctamente")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
strcon.Close()
End Sub
Public Sub eliminarMateria()

"'"

Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = "DELETE FROM materia WHERE codMateria='" & codMateria &
'ejecutar los datos

result = cmd.ExecuteNonQuery
'chequear si los datos han sido eliminados
If result = 0 Then
MsgBox("Ha ocurrido un error al eliminar registro", MsgBoxStyle.Information)
Else
MsgBox("Los datos han sido eliminados")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
strcon.Close()
End Sub
Public Sub cargarMaterias(ByVal cmb As ComboBox)
Try

With cmd
.CommandType = CommandType.Text
.CommandText = "Select nomMateria From materia"
.Connection = strconexion()
End With
da.SelectCommand = cmd
dt = New DataTable
da.Fill(dt)
With cmb
.DataSource = dt
.DisplayMember = "nomMateria"
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

FORMULARIO PRINCIPAL
Public Class Form1
Private Sub registrarDocentes_ItemClick(sender As Object, e As EventArgs) Handles
registrarDocentes.Click
regDocente.Show()
End Sub
Private Sub editarDocentes_ItemClick(sender As Object, e As EventArgs) Handles
editarDocentes.Click

edDocente.Show()
End Sub
Private Sub registrarMateria_ItemClick(sender As Object, e As EventArgs) Handles
registrarMateria.Click
regMateria.Show()
End Sub
Private Sub editarMateria_ItemClick(sender As Object, e As EventArgs) Handles
editarMateria.Click
edMateria.Show()
End Sub
Private Sub cerrar_ItemClick(sender As Object, e As EventArgs) Handles cerrar.Click
MsgBox("Bye Bye")
Me.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class

FORMULARIO DE EDICION DE DOCENTES


Public Class edDocente
Dim docente As New docente
Private _nomDocente, _apeDocente, _direcDocente, _telDocente, _codMateria As String
Private _codDocente As Integer
Public Property codDocente() As Integer
Get
Return Me._codDocente
End Get
Set(value As Integer)
_codDocente = value
End Set
End Property
Public Property nomDocente() As String
Get
Return Me._nomDocente
End Get
Set(value As String)
_nomDocente = value
End Set
End Property

Public Property apeDocente() As String


Get
Return Me._apeDocente
End Get
Set(value As String)
_apeDocente = value
End Set
End Property
Public Property direcDocente() As String
Get
Return Me._direcDocente
End Get
Set(value As String)
_direcDocente = value
End Set
End Property
Public Property telDocente() As String
Get
Return Me._telDocente
End Get
Set(value As String)
_telDocente = value
End Set
End Property
Public Property codMateria() As String
Get
Return Me._codMateria
End Get
Set(value As String)
_codMateria = value
End Set
End Property
Private Sub txtBusqueda_TextChanged(sender As Object, e As EventArgs) Handles
txtBusqueda.TextChanged
docente.listarDocente(dtgDocentes, txtBusqueda, cmbBusqueda)
End Sub
Private Sub cmbBusqueda_SelectedIndexChanged(sender As Object, e As EventArgs)
Handles cmbBusqueda.SelectedIndexChanged
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub edDocente_Load(sender As Object, e As EventArgs) Handles MyBase.Load
docente.listarDocente(dtgDocentes, txtBusqueda, cmbBusqueda)
cmbBusqueda.SelectedIndex = 0
End Sub

Public Sub dtgDocentes_CellContentClick(sender As Object, e As


DataGridViewCellEventArgs) Handles dtgDocentes.CellContentClick
Select Case e.ColumnIndex
Case 6 'En caso de Editar
codDocente = dtgDocentes.Rows(e.RowIndex).Cells(0).Value
nomDocente = dtgDocentes.Rows(e.RowIndex).Cells(1).Value
apeDocente = dtgDocentes.Rows(e.RowIndex).Cells(2).Value
direcDocente = dtgDocentes.Rows(e.RowIndex).Cells(3).Value
telDocente = dtgDocentes.Rows(e.RowIndex).Cells(4).Value
codMateria = dtgDocentes.Rows(e.RowIndex).Cells(5).Value
edDocente1.Show()
Me.Close()
Case 7 'En caso de Eliminar
docente.codDocente = dtgDocentes.Rows(e.RowIndex).Cells(0).Value
docente.eliminarDocente()
docente.listarDocente(dtgDocentes, txtBusqueda, cmbBusqueda)
End Select
End Sub
End Class

FORMULARIO DE EDICION DE MATERIAS


Public Class edMateria
Dim materia As New materia
Private _codigoMateria As Integer
Private _nombreMateria As String
Private _descripcionMateria As String
Public Property codigoMateria() As Integer
Get
Return _codigoMateria
End Get
Set(value As Integer)
_codigoMateria = value
End Set
End Property
Public Property nombreMateria As String
Get
Return _nombreMateria
End Get
Set(value As String)
_nombreMateria = value
End Set
End Property
Public Property descripcionMateria() As String

Get
Return _descripcionMateria
End Get
Set(value As String)
_descripcionMateria = value
End Set
End Property
Private Sub edMateria_Load(sender As Object, e As EventArgs) Handles MyBase.Load
materia.listarMateria(dtgMaterias, txtBusqueda, cmbBusqueda)
cmbBusqueda.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
Public Sub dtgMaterias_CellContentClick(sender As Object, e As
DataGridViewCellEventArgs) Handles dtgMaterias.CellContentClick
Select Case e.ColumnIndex
Case 3 'En caso de Editar
codigoMateria = dtgMaterias.Rows(e.RowIndex).Cells(0).Value
nombreMateria = dtgMaterias.Rows(e.RowIndex).Cells(1).Value
descripcionMateria = dtgMaterias.Rows(e.RowIndex).Cells(2).Value
edMateria1.Show()
Me.Close()
Case 4 'En caso de Eliminar
materia.codMateria = dtgMaterias.Rows(e.RowIndex).Cells(0).Value
materia.eliminarMateria()
materia.listarMateria(dtgMaterias, txtBusqueda, cmbBusqueda)
End Select
End Sub
Private Sub txtBusqueda_TextChanged(sender As Object, e As EventArgs) Handles
txtBusqueda.TextChanged
materia.listarMateria(dtgMaterias, txtBusqueda, cmbBusqueda)
End Sub
End Class

FORMULARIO DE REGISTRO DE DOCENTES


Public Class regDocente
Dim Materia As New materia
Dim Docente As New docente
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub regDocente_Load(sender As Object, e As EventArgs) Handles MyBase.Load

materiaDoc.SelectedItem = 0
Materia.cargarMaterias(materiaDoc)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Docente.nomDocente = nombreDocente.Text
Docente.apeDocente = apellidoDocente.Text
Docente.diDocente = direccionDocente.Text
Docente.teDocente = telefonoDocente.Text
Docente.maDocente = materiaDoc.Text
Docente.guardar()
Me.Close()
End Sub
End Class

FORMULARIO DE REGISTRO DE MATERIAS


Public Class regMateria
Dim materia As New materia
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
materia.nomMateria = nomMateria.Text
materia.desMateria = descripcionMateria.Text
materia.guardarMateria()
Me.Close()
End Sub
Private Sub regMateria_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class

FORMULARIO DE ACTUALIZACION DE DOCENTES


Public Class edDocente1
Dim materia As New materia
Dim docente As New docente
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
edDocente.Show()
Me.Close()
End Sub
Private Sub edDocente1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
materia.cargarMaterias(materiaDoc)
id.Text = edDocente.codDocente
nombreDocente.Text = edDocente.nomDocente
apellidoDocente.Text = edDocente.apeDocente
direccionDocente.Text = edDocente.direcDocente

telefonoDocente.Text = edDocente.telDocente
materiaDoc.Text = edDocente.codDocente
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
docente.codDocente = id.Text
docente.nomDocente = nombreDocente.Text
docente.apeDocente = apellidoDocente.Text
docente.teDocente = telefonoDocente.Text
docente.diDocente = direccionDocente.Text
docente.maDocente = materiaDoc.Text
Try
docente.actualizarDocente()
Me.Close()
edDocente.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

FORMULARIO DE ACTUALIZACION DE MATERIAS


Public Class edMateria1
Dim edMaterias As New edMateria
Dim materia As New materia
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
edMateria.Show()
End Sub
Private Sub edMateria1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
code.Text = edMateria.codigoMateria
nomMateria.Text = edMateria.nombreMateria
descripcionMateria.Text = edMateria.descripcionMateria
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
materia.nomMateria = nomMateria.Text
materia.desMateria = descripcionMateria.Text
materia.codMateria = code.Text
Try
materia.actualizarMateria()
Me.Close()
edMateria.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class