【问题标题】:Specified cast is not valid while processing fieldreader处理 fieldreader 时指定的强制转换无效
【发布时间】:2014-07-09 11:55:59
【问题描述】:

当我尝试将 fieldreader 设置为 int 数据类型时,发生指定的强制转换无效。

_userid 在数据库中属于int 类型

var _fieldReader = sqlCmd.ExecuteReader();

while (_fieldReader.Read())
{
      _userId = _fieldReader.GetInt16(0);
}
sqlCon.Close();

谁能帮我解决这个问题?

【问题讨论】:

  • 使用GetInt32 而不是GetInt16
  • GetInt16 说“我希望这是一个 16 位整数”;如果它不是,那么:不要使用那个;如果它实际上是int,请使用GetInt32

标签: c# asp.net-mvc sql-server-2008


【解决方案1】:

如果 user_id 在您的数据库中是 int 类型,则它与 32 位整数匹配。请改用GetInt32()

 _userId = _fieldReader.GetInt32(0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2014-07-30
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多