将类型为System.Int64的对象无法转换为类型System.Int32

3

我收到一个报错:无法将类型为'System.Int64'的对象强制转换为类型'System.Int32',出现在以下位置:

item.ItemCount = reader.GetInt32(reader.GetOrdinal("amount"));

我尝试过:

item.ItemCount = reader.GetInt64(reader.GetOrdinal("amount"));

但我得到了:
Error   CS0266  
Cannot implicitly convert type 'long' to 'int'. 
An explicit conversion exists (are you missing a cast?)

这个字段是 bigint 类型,这是我第一次使用 .Net。

1个回答

5
你需要使用 GetInt64
item.ItemCount = reader.GetInt64(reader.GetOrdinal("amount"));

bigint是 SQL 中与 .NET long 相当的数据类型,GetInt64 返回一个 long,而 GetInt32 返回一个 int

有关更多详细信息,请参见此文档


错误 CS0266 无法隐式将类型 'long' 转换为 'int'。存在显式转换(是否缺少强制转换?) - Back Office
你需要将 ItemCount 属性修改为 long 类型。 - User2585
我不确定如何做到那个。 - Back Office
我认为这就是一个 InventoryInfo 对象 = 新的 InventoryInfo(); - Back Office
List<InventoryInfo> inventoryList = new List<InventoryInfo>(); 清单<库存信息>库存列表=新的清单<库存信息>(); - Back Office
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接