gpt4 book ai didi

c# - 处理从数据库返回的空日期

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

您好,我正在尝试返回一个可能具有空日期值的集合。但是,如果它们为 null - 我希望保持这种状态,而不仅仅是使用任意日期。

如果它是一个字符串,我会像这样检查空值:

calEventDTO.occurrenceType = dr.IsDBNull(10) ? null : dr.GetString(10);

我如何为约会做同样的事情?以下不编译('null' 和 System.DateTime' 之间没有隐式转换)。

calEventDTO.recurrenceID = dr.IsDBNull(9) ? null : dr.GetDateTime(9);

我的 dto 设置为处理空值。

public DateTime? recurrenceID
{
get { return _recurrenceID; }
set { _recurrenceID = value; }
}

非常感谢任何提示/帮助/指针。谢谢

最佳答案

尝试:

calEventDTO.recurrenceID = dr.IsDBNull(9) ? null : (DateTime?) dr.GetDateTime(9);

那个?运算符要求两个操作数(在本例中为 null 和 dr.GetDateTime(9))属于同一类型。

关于c# - 处理从数据库返回的空日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1089774/

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