MySqlDataReader cast function, wpf

160 Views Asked by At

I want to cast reader from mysqldatareader to list of specific object. So i use the function cast but it doesn't work, i have an exception. enter image description here

This is the class of objectenter image description here

1

There are 1 best solutions below

0
On

You can't cast a MySqlDataReader to a List<T>. You should use the reader to read the records from the database and create a KeyError object for each record that you add to the list. Something like this:

List<KeyError> list = new List<KeyError>();
using (MySqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        list.Add(new KeyError()
        {
            StartDate = reader.GetString("StartDate"),
            StartDate = reader.GetDateTime("ServerDate")
            //...
        });
    }
}

The MySqlDataReader class has methods for getting the value of a specified column in the result set as any of the primitive C# data types: https://dev.mysql.com/doc/dev/connector-net/8.0/html/T_MySql_Data_MySqlClient_MySqlDataReader.htm