Jeppe Christensen Jeppe Christensen - 1 month ago 13
MySQL Question

Reader doesn't return all rows from database

So i have a web api that presents values from my database. However i have 6 rows in my database, but it doesn't return the first row.

How do i get it to read and return all rows in my database?

cmd.CommandText = "SELECT * FROM `CustomerDb`";
cmd.CommandType = CommandType.Text;
cmd.Connection = mySqlConnection;

mySqlConnection.Open();
MySqlDataReader reader = cmd.ExecuteReader();

var listOfPlace = new List<Place>();

if (reader.Read())
{
while (reader.Read())
{
var Place = new Place();
Place.ID = Convert.ToInt32(reader["id"]);
Place.Name = reader["name"].ToString();
Place.Address = reader["address"].ToString();
Place.City = reader["city"].ToString();
Place.Category = reader["category"].ToString();

listOfPlace.Add(Place);
}
};
return listOfPlace;

Answer

Remove if block.

if (reader.Read())
    {