DateTime问题 [英] DateTime problem

查看:77
本文介绍了DateTime问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,


我总是在努力解决以下问题:


我有一个对象obj,其属性为paydate(type = DateTime)

现在,在实例化对象并执行几项操作之后,我还要检查我的paydate是否仍然是空的。

我该怎么做?


我尝试过几样的事情(都有错误):

if(obj.paydate == null)

if(obj.paydate.gettype( )== System.DBnull)

if(obj.paydate == 01/01/0001)

等.....


另一个问题:

当我想要实例化对象并用sql-query(sql-database)中的数据填充它时。从sql for paydate = null接收的值。

如何在datareader中测试从sql接收的数据是否为空,因此我没有在obj.paydate中放置任何内容。

tkx,

Nic

解决方案

nic< ni*@discussions.microsoft.com>写道:

我总是在努力解决以下问题:

我有一个带有属性paydate的对象obj(type = DateTime)
现在,在实例化对象之后做几件事,我也想要检查我的工资是否仍然是空的。


开始时并不是空的。对于值

类型,没有等价的null。有些人选择使用DateTime.MinValue作为特殊的值

等效于DateTimes的null。其他人使用单独的财产

表明DateTime是否是真实的。一个。

另一个问题:
当我想实例化对象并用
sql-query(sql-database)中的数据填充它时。从sql收到的值为paydate =
null。
如何在datareader中测试从sql收到的数据是否为空,所以我什么也没放入obj.paydate。



使用DataReader中的IsDbNull()。在这种情况下,你仍然需要工作

什么才能投入到更新状态。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www.pobox.com/~skeet

如果回复小组,请不要给我发邮件





如果您在班级

构造函数中将paydate初始化为PayDate = new DateTime(),您可以将paydate与DateTime.MinValue进行比较以检查它是否

仍然具有初始值。唯一的问题是,如果用户

将paydate设置为01/01/0001 00:00:00,那么您将无法检测到

更改为DateTime.MinValue只是01/01/0001 00:00:00。


或者,您可以在设置访问器中维护自己的脏标志

paydate的方法并检查该标志以检测任何更改。


要检查null,请使用SqlDataReader.IsDBNull()


HTH。


" nic" < ni*@discussions.microsoft.com>在消息中写道

news:95 ********************************** @ microsof t.com ...

嘿,


我总是在努力解决以下问题:


我有一个具有属性paydate的对象obj(type = DateTime)

现在,在实例化对象并做了几件事之后,我想要检查

如果我的paydate仍然是空的。

我该怎么做?


我尝试了几件事(如有错误):

if(obj.paydate = = null)

if(obj.paydate.gettype()== System.DBnull)

if(obj.paydate == 01/01/0001)

等.....


另一个问题:

当我想要实例化对象并用
sql-query(sql-database)。从sql for paydate = null接收的值。

如何在datareader中测试从sql接收的数据为空并且

所以我什么都没放在obj.paydate中。

tkx,

Nic


hey,

I always struggle with the following:

I have an object obj with a property paydate (type = DateTime)
Now, after instantiating the object and do several things, I want too check if my paydate is still empty.
How can I do this?

I tried several things like (all with errors):
if (obj.paydate == null)
if (obj.paydate.gettype() == System.DBnull)
if (obj.paydate == 01/01/0001)
etc.....

Another question :
When I want to instantiate the object and fill it with data from an sql-query (sql-database). The value received from sql for paydate = null.
How can I test in the datareader that the received data from sql is null and so I put nothing in obj.paydate.
tkx,
Nic

解决方案

nic <ni*@discussions.microsoft.com> wrote:

I always struggle with the following:

I have an object obj with a property paydate (type = DateTime)
Now, after instantiating the object and do several things, I want too
check if my paydate is still empty.
It''s not empty to start with. There''s no equivalent for null for value
types. Some people choose to use DateTime.MinValue as a "special" value
equivalent to null for DateTimes. Others use a separate property to
indicate whether or not the DateTime is a "real" one.
Another question :
When I want to instantiate the object and fill it with data from an
sql-query (sql-database). The value received from sql for paydate =
null.
How can I test in the datareader that the received data from sql is
null and so I put nothing in obj.paydate.



Use the IsDbNull() in the DataReader. You''ll still have to work out
what to put into paydate in that case though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hi,

If you initalize the paydate as PayDate = new DateTime() in the class
constructor, you can compare paydate with DateTime.MinValue to check if it
still has the initial value. The only problem with this is that if the user
sets paydate to 01/01/0001 00:00:00, then you wont be able to detect the
changes as DateTime.MinValue is nothing but 01/01/0001 00:00:00.

Alternatively, you can maintain your own dirty flag in the set accessor
method of the paydate and check that flag to detect any changes.

For checking null, use SqlDataReader.IsDBNull()

HTH.

"nic" <ni*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
hey,

I always struggle with the following:

I have an object obj with a property paydate (type = DateTime)
Now, after instantiating the object and do several things, I want too check
if my paydate is still empty.
How can I do this?

I tried several things like (all with errors):
if (obj.paydate == null)
if (obj.paydate.gettype() == System.DBnull)
if (obj.paydate == 01/01/0001)
etc.....

Another question :
When I want to instantiate the object and fill it with data from an
sql-query (sql-database). The value received from sql for paydate = null.
How can I test in the datareader that the received data from sql is null and
so I put nothing in obj.paydate.
tkx,
Nic


这篇关于DateTime问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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