如何为日期时间类型的类对象分配空值 [英] How to assign null value to class object of type datetime

查看:86
本文介绍了如何为日期时间类型的类对象分配空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有班级人员.在那个死亡日期字段中存在.有时它为null.所以当我从数据库中获取它时,我想为其分配空值.
例如objperson._deathdate =?.我想给它赋空值.我该怎么办?

i have class person.In that death date field is present.Sometimes it is null.so when i getting it from database i want to assign null value to it.
e.g. objperson._deathdate = ?. i want to assign null value to it. What can i do?

推荐答案

如果您的数据库字段定义允许死亡日期为空,那么当您将其读回时,它将以System.DBNull
读取数据库行时进行检查,然后将null分配给DateTime变量.
If your database field definition allows nulls for the Death date, then when you read it back, it will come back as System.DBNull
Check for this when you read the databse row, and assign null to your DateTime variable.
objperson._deathdate = null;




如果不是,那么为有生命的人指定一个默认的死亡日期,该日期可能是:
1)过去
2)与出生日期相同
3)将来很远
然后,您可以检查并指定一个空值.

优选地,在数据库字段定义中为所有可选字段(例如,死亡日期)允许一个空值.

我只是意识到可能不是数据库方面给您带来了麻烦.

您是否已将DateTime声明为可空类型?如果不是,那么您将永远无法为其分配一个真正的null.
如果不是,则将其声明为:




If it doesn''t, then for living people assign a default death date, which could be:
1) Far in the past
2) Same as the Birth date
3) Far in the future
You can then check for this and assign a null instead.

By preference, allow a null in the database field definition for all fields which are optional, such as the death date.

I just realised that it may not be the database side that is giving you problems.

Have you declared your DateTime as a nullable type? If not, then you can never assign a true null to it.
If not, then declare it as:

DateTime? _deathdate;

和null的分配将起作用.

[edit]添加了可为空的类型信息-OriginalGriff [/edit]

and the assignment of null will work.

[edit]Added nullable type info - OriginalGriff[/edit]


http: //stackstackflow.com/questions/221732/datetime-null-value [ ^ ]
http://www.velocityreviews.com/forums/t301163-how-to-set-datetime-variable-to-null-in-c.html [
http://stackoverflow.com/questions/221732/datetime-null-value[^]
http://www.velocityreviews.com/forums/t301163-how-to-set-datetime-variable-to-null-in-c.html[^]

cheer:thumbsup:


这篇关于如何为日期时间类型的类对象分配空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆