I've been trying to read data from my table Employee (7 columns) with 3 employees registered, and DataReader just read first row (7 fields instead the total, 21 fields)
[HttpGet]
[Route("GetEmployees")]
public List<clsEmployee> GetEmployees()
{
MySqlDataReader reader;
List<clsEmployee> employees = new List<clsEmployee>();
string strSQL = "SELECT e.EmployeeID, e.FirstName, e.LastName, e.email, e.dni, e.phone, e.SpecialityID from Employee e";
reader = CmdDatabase.GetReader(strSQL, configuration.GetConnectionString("da_adminsys"));
while (reader.Read())
{
clsEmployee emp = new clsEmployee
{
EmployeeID = Convert.ToInt32(reader[0]),
FirstName = reader["firstname"].ToString(),
LastName = reader["lastname"].ToString(),
Email = reader["email"].ToString(),
Phone = reader["phone"].ToString(),
DNI = reader["dni"].ToString(),
SpecialityID = Convert.ToInt32(reader["SpecialityID"]),
};
employees.Add(emp);
}
foreach (var item in employees)
{
clsSpeciality speciality = GetSpeciality(item.SpecialityID);
item.Speciality.Add(speciality);
}
reader.Close();
return employees;
}
And GetReader from my class CmdDatabase:
public static MySqlDataReader GetReader(string strQuery, string CN)
{
MySqlConnection oConnection = new MySqlConnection(CN);
MySqlCommand oCommand = new MySqlCommand(strQuery, oConnection);
oConnection.Open();
MySqlDataReader oReader;
oReader = oCommand.ExecuteReader(CommandBehavior.CloseConnection);
oCommand.Dispose();
return oReader;
}
At the while loop its just counting 7 FieldCount, and totally in the table are 21 fields
You are doing this wrong, as you are disposing the command before finshing reading from the reader.
You need to have the conenction, command and reader all in
using.I have no idea what your
GetSpecialitydoes, but if it's a database call then you are much better off using a single database call with a join.Also consider using
asyncespecially given that this is in the middle of an ASP.Net HTTP request. That would look like this