想要为Datetime传递null值 [英] Want to pass null value for Datetime

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

问题描述

我想为datetime对象传递null值。我试试DateTime? dt = null。

所以,这里我还要写DateTime?在我不想要的方法中。所以我尝试另一个,即我将DBNull.Value分配给datetime对象。但它给出了一个错误,'无法将类型'System.DBNull隐式转换为System.DateTime'。

我该怎么办?

解决方案

< blockquote>如果选中Allow Nulls字段。我认为以下链接可以帮助您:

http:// www .c-sharpcorner.com / UploadFile / sd_patel / EnterNullValuesForDateTime11222005015742AM / EnterNullValuesForDateTime.aspx [ ^ ]



http://social.msdn.microsoft.com/Forums/en-US/devdocs/thread/d5a42234-0b9e-4a8e-9bfa -354869e74802 / [ ^ ]



如需了解更多信息,请浏览以下链接:



http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx [ ^ ]

http://msdn.microsoft.com/en-us /library/system.dbnull.aspx [ ^ ]



如果我误解了你的问题,请随时纠正我。

我希望以上信息会有所帮助。如果您有更多疑虑,请告诉我。


DateTime数据类型不应为null。而是使用 DateTime.Minvalue 存储最小值。


值类型,例如 System.DataTime 无法接受 null DBNull 值。此类型也应该有一个值。



有一种流行的方法,使用 DateTime.MinValue 。这就像是另一种编程欺诈。 MinValue 表示0001年1月1日00:00:00.0000000的时间点。为什么地球上的这个时间应被视为无效(稍后一毫秒的时间有效)?!



我认为只有一个不错的解决方案:使用从 DataTime派生的可空类型



 System.DataTime? MyDateTime; 

// MyDateTime可以分配为null或vSystem.DateTime值:
MyDateTime = null ; // 如果数据库返回DBNull
// ...
MyDateTime = DateTime.Now; // 或任何有效的有效System.DateTime值





有关可空类型的更多信息,请参阅 http:// msdn .microsoft.com / zh-CN / library / 1t3y8s4s.aspx [ ^ ]。

引入可空类型的主要目的是处理类似于本课题中描述的情况。

-SA


I want to pass null value for datetime object. i try DateTime? dt= null.
so, here also i have to write DateTime? in method which i don't want. so i try another i.e. i assign DBNull.Value to datetime object. but it gives an error that 'Cannot implicitly convert type 'System.DBNull to System.DateTime'.
so what can i do?

解决方案

If Allow Nulls is checked for the field. I think the following links will help you:
http://www.c-sharpcorner.com/UploadFile/sd_patel/EnterNullValuesForDateTime11222005015742AM/EnterNullValuesForDateTime.aspx[^]

http://social.msdn.microsoft.com/Forums/en-US/devdocs/thread/d5a42234-0b9e-4a8e-9bfa-354869e74802/[^]

For further information please navigate the following links:

http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx[^]
http://msdn.microsoft.com/en-us/library/system.dbnull.aspx[^]

If i misunderstand your question, please feel free to correct me.
I hope the above information will be helpful. If you have more concerns, please let me know.


DateTime datatype shouldn't be null. Instead use DateTime.Minvalue which stores the minimum value.


A value type such as System.DataTime cannot accept null or DBNull value. This type should also have a value.

There is a popular method of using DateTime.MinValue. This is nothing like yes another programming fraud. MinValue represents the time point of 00:00:00.0000000, January 1, 0001. Why on Earth this time should be considered as invalid (while a time of one millisecond later is valid)?!

I think there is only one decent solution: use a nullable type derived from DataTime.

System.DataTime? MyDateTime;

//MyDateTime can be assigned to null or vSystem.DateTime value:
MyDateTime = null; //if database returns DBNull
//...
MyDateTime = DateTime.Now; //or any valid valid System.DateTime value



For more information on nullable types, see http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx[^].
A primary purpose of introduction of nullable types was handling of the situations similar to the one described in the present Question.

—SA


这篇关于想要为Datetime传递null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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