0% found this document useful (0 votes)
15 views2 pages

Conectar MySql

Uploaded by

caradeveligne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Conectar MySql

Uploaded by

caradeveligne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Imports MySql.Data.

MySqlClient
Public Class Form1
Dim conexion As New MySqlConnection("server=localhost; user=root; password=; database=alumno")
Dim adaptador As MySqlDataAdapter
Dim dt As DataTable

Private Sub btnConectar_Click(sender As Object, e As EventArgs) Handles btnConectar.Click


Try
conexion.Open()
MsgBox("CONECTADO")
conexion.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub CargarDatos()


Try
conexion.Open()
Dim query As String = "SELECT * FROM alumno"
adaptador = New MySqlDataAdapter(query, conexion)
dt = New DataTable()
adaptador.Fill(dt)
DataGridView1.DataSource = dt
Catch ex As Exception
MsgBox(ex.Message)
Finally
conexion.Close()
End Try
End Sub

Private Sub btnCargar_Click(sender As Object, e As EventArgs) Handles btnCargar.Click


' Llamar a la función para cargar los datos cuando se presiona un botón (opcional)
CargarDatos()
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnSalir.Click


End
End Sub

Private Sub btnCargardatos_Click(sender As Object, e As EventArgs) Handles btnCargardatos.Click


Try
conexion.Open()
Dim query As String = "INSERT INTO alumno (id_alumno, nombre, edad) VALUES (@id_alumno,
@nombre, @edad)"
Dim comando As New MySqlCommand(query, conexion)
comando.Parameters.AddWithValue("@id_alumno", Integer.Parse(txtId_Alumno.Text))
comando.Parameters.AddWithValue("@nombre", txtNombre.Text)
comando.Parameters.AddWithValue("@edad", Integer.Parse(txtEdad.Text))
comando.ExecuteNonQuery()
MsgBox("Registro agregado exitosamente")
Catch ex As Exception
MsgBox(ex.Message)
Finally
conexion.Close()
' Recargar los datos en el DataGridView
CargarDatos()
End Try
End Sub
Private Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click
' Verificar si hay una fila seleccionada
If DataGridView1.SelectedRows.Count > 0 Then
' Obtener el índice de la fila seleccionada
Dim rowIndex As Integer = DataGridView1.SelectedRows(0).Index

' Obtener el id_alumno de la fila seleccionada


Dim idAlumno As Integer = DataGridView1.SelectedRows(0).Cells("id_alumno").Value

' Eliminar la fila de la base de datos


Try
conexion.Open()
Dim query As String = "DELETE FROM alumno WHERE id_alumno = @id_alumno"
Dim comando As New MySqlCommand(query, conexion)
comando.Parameters.AddWithValue("@id_alumno", idAlumno)
comando.ExecuteNonQuery()
MsgBox("Registro eliminado exitosamente")
Catch ex As Exception
MsgBox(ex.Message)
Finally
conexion.Close()
End Try

' Eliminar la fila del DataTable


dt.Rows(rowIndex).Delete()

' Actualizar el DataGridView


DataGridView1.DataSource = dt
Else
MsgBox("Por favor, seleccione una fila para eliminar.")
End If
End Sub

Private Sub btnLimpiar_Click(sender As Object, e As EventArgs) Handles btnLimpiar.Click


txtId_Alumno.Text = ""
txtNombre.Text = ""
txtEdad.Text = ""
' Verificar si el DataGridView está asociado a un DataSource
If DataGridView1.DataSource IsNot Nothing Then
' Limpiar el DataTable asociado
Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
dt.Rows.Clear()
Else
' Limpiar las filas del DataGridView si no está asociado a un DataSource
DataGridView1.Rows.Clear()
End If
End Sub
End Class

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy