Ya hemos visto como realizar la conexión a la base de datos MySQL / MariaDB además de conectarnos con una cadena de conexión dinámica ahora veremos como realizar una consulta a una table y leer los datos con el ciclo While.

Estamos en en el archivo modulo crearModelo.vb justo despues de abrir la conexión declaramos la variable comando de tipo mysqlcommand, y ejecutaremos la siguiente consulta “describe proveedores;” la cual nos regresa los campos de la tabla, quedaría de la siguiente forma.

 Dim comando = New MySqlConnector.MySqlCommand("describe proveedores;", connection)

Posteriormente guardamos el resultado usando la función executeReader

Dim resultado = comando.ExecuteReader

En resultado tenemos guardado todos los renglones y columnas que nos regreso la consulta “describe proveedores;” para leerlos y mostrarlos en un MessageBox necesitaremos recorrerlos mediante un ciclo while y lo hacemos de la siguiente forma.

   While (resultado.Read)

      MessageBox.Show(resultado.GetString(0))

   End While
Vemos como el messageBox se muestra dependiendo del numero de columnas que tenga la tabla a la cual le hicimos el describe

Al final el código quedaría de la siguiente forma

'IMPORTAMOS LAS LIBRERIAS NECESARIAS PARA LA CONEXIÓN A MYSQL/MARIADB
Imports MySqlConnector.MySqlConnection

'EMPIEZA EL MODULO
Module crearModelo

    ' CON PUBLIC SUB CREAMOS PROCEDIMIENTOS
    Public Sub generaModelo()

        ' VARIABLES PARA LA CONEXIÓN
        Dim strServidor As String
        Dim strBaseDeDatos As String
        Dim strUsuario As String
        Dim strContra As String


        'OBTENEMOS LOS DATOS DE CONFIGURACIÖN DEL REGISTRO DE WINDOWS PREVIAMENTE GUARDADAS
        If Not My.Computer.Registry.GetValue("HKEY_CURRENT_USER\CREADORMVCPHP", "servidor", Nothing) Is Nothing Then
            strServidor = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\CREADORMVCPHP", "servidor", Nothing).ToString()
            strBaseDeDatos = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\CREADORMVCPHP", "baseDeDatos", Nothing).ToString()
            strUsuario = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\CREADORMVCPHP", "usuario", Nothing).ToString()
            strContra = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\CREADORMVCPHP", "contra", Nothing).ToString()
        Else

            MsgBox("No se han capturado la conexión a la base de datos")
            Return

        End If


        ' METEMOS LA CADENA DE CONEXIÓN EN UNA VARIABLE
        Dim cadenaConexion = "Server=" & strServidor & ";User ID=" & strUsuario & ";Password=" & strContra & ";Database=" & strBaseDeDatos

        ' CREAMOS LA CONEXIÓN CON LA CADENA DE CONEXIÓN
        Dim connection = New MySqlConnector.MySqlConnection(cadenaConexion)

        Try

            ' SI SE ABRE LA CONEXIÓN MANDAMOS MENSAJE
            connection.Open()


            Dim comando = New MySqlConnector.MySqlCommand("describe proveedores;", connection)

            Dim resultado = comando.ExecuteReader

            While (resultado.Read)

                MessageBox.Show(resultado.GetString(0))


            End While


            MsgBox("Conexion Correcta")

        Catch ex As Exception
            ' SI NO SE ABRE LA CONEXIÓN MANDAMOS MENSAJE DE ERROR
            MsgBox("ERROR " & ex.Message)

            Exit Sub
        End Try

    End Sub

End Module