简体   繁体   中英

I want to put all the database data in the list

 List<int> values = new List<int>();

string sql = "SELECT Values_To_Add FROM table";

command.CommandText = sql;

MySqlDataReader reader = command.ExecuteReader(); 
while(reader.Read())
{
    values.Add(reader["Values_To_Add "]);
}

Error CS1503 Argument 1: cannot convert from 'object' to 'int'

Someone's idea?

You should cast the value from the reader as integer. You can do it safely like this:

 var intValue = reader["Values_To_Add"] as int?;
 if (intValue != null) { values.Add(intValue.Value); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM