gpt4 book ai didi

C# 处理空值

转载 作者:太空宇宙 更新时间:2023-11-03 17:22:34 24 4
gpt4 key购买 nike

我将 CustomerID 声明为

int? CustomerID=null;

我在读取 DataReader 时检查空值

Id = reader["CustomerId"] is DBNull ? null :Convert.ToInt32(reader["CustomerID"]);

正在 throw

 Type of conditional expression cannot be determined because there 
is no implicit conversion between '<null>' and 'int'

转换有什么问题?

最佳答案

我认为你需要这样做:

if(! reader.IsDBNull(reader.GetOrdinal("CustomerId"))
{
Id = Convert.ToInt32(reader["CustomerID"]);
}
else
{
Id = NULL;
}

您需要在读取器上使用 .IsDBNull 方法来提前确定列是否为 NULL - 如果是,甚至不要从读取器读取值。

关于C# 处理空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3987607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com