Question Getting data from MySQL

eltincho2202

Member
Joined
Jul 26, 2010
Messages
8
Programming Experience
1-3
I would like to know how can I get specific data with mysql and vb.net

What I want to do is this:

I have a class... lets call it Animal. In my MySQL database I have one table called animal with some fields. Each field is an attribute of the Animal class.

What I want to do is to execute a SQL like "select * from animal" and set all de data in the specific attributes, for example:

table animal:
animalid integer
animalname varchar(100)
animaltype varchar(100)


Dim myAnimal as New Animal()

myAnimal.Id = animalid
myAnimal.Name = animalname
myAnimal.Type = animaltype


I want to create a function that returns me the myAnimal object.

How can I set those values to my Animal object?
 
You would execute your query and create a MySqlDataReader. You would then use a Do or While loop to read the entire result set. Each iteration, you would create an Animal object, set its properties from the fields of the data reader and then add it to a collection.
 
VB.NET:
Dim animals As New List(Of Animal)

Using connection As New MySqlConnection("connection string here")
    Using command As New MySqlCommand("SQL query here", connection)
        connection.Open()

        Using reader = command.ExecuteReader()
            While reader.Read()
                Dim a As New Animal

                'Set properties of a here.

                animals.Add(a)
            End While
        End Using
    End Using
End Using
 
Back
Top