如何将 null 作为 DateTime 值传递?
在数据库中,有一个字段保存关闭日期。仅当案件已结案时,此日期才能为 NOT NULL。如果案例未关闭,则必须为 NULL。如何将 null 值传递给 DateTime 对象?
尝试过这个,但它不起作用。
DateTime closure= dateDatumIspisa.SelectedDate ?? null;
DateTime closure= dateDatumIspisa.SelectedDate ?? DateTime.Parse("");
DateTime closure= dateDatumIspisa.SelectedDate ?? DBNull.Value;
DateTime closure= dateDatumIspisa.SelectedDate ?? DateTime.Parse(DBNull.Value.ToString());
还尝试了 GetValueOrDefault() 但它插入了 DateTime.Min 值,而我需要将此字段留空。
有什么建议吗?
In a database, there is a field that saves a closure date. This date can be NOT NULL only if the case has been closed. If the case is not closed, it has to be NULL. How can I pass null value to a DateTime object?
Tried this but it doesn't work.
DateTime closure= dateDatumIspisa.SelectedDate ?? null;
DateTime closure= dateDatumIspisa.SelectedDate ?? DateTime.Parse("");
DateTime closure= dateDatumIspisa.SelectedDate ?? DBNull.Value;
DateTime closure= dateDatumIspisa.SelectedDate ?? DateTime.Parse(DBNull.Value.ToString());
Also tried GetValueOrDefault() but it inserts DateTime.Min value, while I need this field left empty.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将
closure
设为DateTime?
而不是DateTime
即可。Nullable
的全部要点是从不可为空的类型创建可空的类型。现在,您还没有显示
SelectedDate
的类型 - 但如果它已经是DateTime?
那么您不需要使用??
根本不。 Just:您对可空值类型的熟悉程度如何?您可能需要阅读MSDN 的相关报道。
Just make
closure
aDateTime?
instead of aDateTime
. The whole point ofNullable<T>
is to make a nullable type from a non-nullable one.Now, you haven't shown the type of
SelectedDate
- but if it's already aDateTime?
then you don't need to use??
at all. Just:How familiar are you with nullable value types in general? You might want to read up on the MSDN coverage of them.
声明
不需要在此行中使用 ?? !
Declare
no need here to use the ?? in this line !