❔ Best practice to retrieve MySQL columns

Hello wonderful people! Can you please help me understanding what would be the best practice to retrieve values of specific columns when I use MySQL query? Currently, I just hardcode names but this doesn't seem to be the ideal situation. I see that I can create an enum for the names and use Enum.Login.ToString() Or I can index it (like reader[0]) But both options has its flows as I can change the column name / change the order. What is the best practice to get specific columns? Or what method you personally use for a smaller scale application?
while (reader.Read())
{
User user = new User
{
Login = (string)reader["login"],
TeacherFlag = Convert.ToBoolean(reader["teacher_flag"]),
AdminFlag = Convert.ToBoolean(reader["admin_flag"])

};
Login.SetCurrentUser(user);
}
while (reader.Read())
{
User user = new User
{
Login = (string)reader["login"],
TeacherFlag = Convert.ToBoolean(reader["teacher_flag"]),
AdminFlag = Convert.ToBoolean(reader["admin_flag"])

};
Login.SetCurrentUser(user);
}
2 Replies
Angius
Angius2y ago
It'd be best to use something like Dapper to map the results of the query to objects
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.