【问题标题】:I want to put all the database data in the list我想把所有的数据库数据放在列表中
【发布时间】:2019-01-01 13:55:16
【问题描述】:
 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 "]);
}

错误 CS1503 参数 1:无法从 'object' 转换为 'int'

某人的想法?

【问题讨论】:

  • 投射到int? int.TryParse() 字段值?使用DataTable?

标签: c# mysql database list


【解决方案1】:

您应该将来自阅读器的值转换为整数。 您可以像这样安全地进行操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多