Search code examples
vb.netmariadbmysql.data

How do i get all columns / rows from my Database [VB.Net/MariaDB]


I am trying to get all results with the MySQL.Data Nuget because I am using MariaDB. But my Method just provides the first entry in my DB and does nothing more.

Public Function getAllFields(ByVal sql As String) As List(Of String)
        Dim output As List(Of String) = New List(Of String)

        Using cn = New MySqlConnection(connString.ToString())

            Using cmd = New MySqlCommand(sql, cn)
                cn.Open()

                Using rd = cmd.ExecuteReader()
                    rd.Read()

                    Dim objs(rd.FieldCount) As Object
                    Dim quant As Integer = rd.GetValues(objs)
                    Dim i As Integer
                    For i = 0 To quant - 1
                        output.Add(objs(i))
                    Next i
                    rd.Close()
                End Using

                cn.Close()
            End Using
        End Using

        Return output

    End Function

Solution

  • The call to rd.Read returns True when data is read, and False otherwise. So you need to loop until it returns False.

    Using rd = cmd.ExecuteReader()
        While rd.Read()
            Dim objs(rd.FieldCount) As Object
            Dim quant As Integer = rd.GetValues(objs)
            Dim i As Integer
            For i = 0 To quant - 1
                output.Add(objs(i))
            Next i
        End While
    End Using