如何检查是否未分配DateTime变量? [英] How to check if DateTime variable is not assigned?

查看:91
本文介绍了如何检查是否未分配DateTime变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查是否未分配DateTime变量?

我想将一个"Myobject"对象传递给我的函数.像这样:

How to check if DateTime variable is not assigned?

I want to pass a ''Myobject'' object into my function. Like this:

public class Myobject
{
    public string Code { get; set; }
    public DateTime? StartDate { get; set; }

...



但是..该对象可能未分配StartDate.因此,我将从SQL中收到这样的错误:"SqlDateTime溢出.必须介于...之间."

我尝试过那样,但是没用:



But.. It is possible that this object will have not assigned StartDate. So, I will get an error from SQL like this: "SqlDateTime overflow. Must be between ..."

I tried like that, but it didn''t work:

...

using (SqlCommand sc = new SqlCommand("AddSchedule", sqlConnection))
{
    sc.CommandType = CommandType.StoredProcedure;
    sc.CommandTimeout = 100;
    sc.Transaction = transaction;

    if (_schedule.StartDate.HasValue)
    {
        sc.Parameters.Add(new SqlParameter("@inStartDate", _schedule.StartDate));
    }
    else
    {
        sc.Parameters.Add(new SqlParameter("@inStartDate", DBNull.Value));
    }
 
    using (sc.ExecuteReader())
    {
    }

}



请帮忙.

-------------------------------------------------- --------------------------------------------------

我仍然无法解决:/这太奇怪了!
再一次,非常简单的代码:



Please help.

----------------------------------------------------------------------------------------------------

I still can''t solve that:/ This is... weird!
Once again, VERY SIMPLE code:

public class Myobject
{
    public string Code { get; set; }
    public DateTime? StartDate { get; set; }
}

and PROGRAM:
MyObject mo = new MyObject();
mo.Code= "sth";
// NO action on StartDate property! 
 
    if (mo.StartDate.HasValue)
    {
        sc.Parameters.Add(new SqlParameter("@inStartDate", mo.StartDate.Value));
    }
    else
    {
        sc.Parameters.Add(new SqlParameter("@inStartDate", DBNull.Value));
    }




mo.StartDate.HasValue始终返回true ...
到底是什么?



我想要的只是这个如果":
如果(mo.StartDate.HasValue)
告诉我的是,是否使用了对象的DateTime属性-这就是我想要的.必须有一个简单的解决方案..

我什至尝试过





mo.StartDate.HasValue returns always true...
What the hell ?



All I want is this ''if'':
if (mo.StartDate.HasValue)
Something which tell me, if the DateTime property of the object was used or not - thats all I want. There must be an easy solution..

I even tried


MyObject mo = new MyObject();

if ((mo.StartDate.Value) == null) // This returns FALSE! HOW?!
{
    sc.Parameters.Add(new SqlParameter("@inStartDate", DBNull.Value));
}

推荐答案

您还必须注意,SQL DateTime的范围小于.NET类型的范围.

特别是,如果您使用的是smalldatetime,则默认值为DateTime 的值将超出SQL范围.

对于正常的SQL日期时间,使用DateTime.MaxValue会出现问题,因为SQL类型不太精确.舍入到SQL精度后,该值将为rangle,因为它将舍入到10000年.
You also have to be aware that the range of SQL DateTime is smaller than the range of .NET type.

In particular if you are using smalldatetime, then a DateTime with it default value would out of SQL range.

For normal SQL datetime, there is a problem using DateTime.MaxValue since SQL type is less precise. After rounding to SQL precision, the value would be of of rangle since it would round to year 10000.


尝试使用_schedule.StartDate.Value而不是_schedule.StartDate,并确保相应的SP参数为可以为空.
Try _schedule.StartDate.Value instead of _schedule.StartDate, and make sure the corresponding SP parameter is nullable.


您正在检查日期时间是否有值,但是SQL代码也会检查.例如,如果@date为null,则使用null,以检查是否传递了null,是否需要编写特殊情况代码来处理该问题.据我所知,真正的问题在于您的SP中,如果您的值为null,则您正在检查并传递null,因此您在C#中的代码很好.
You''re checking if your datetime has a value, but does your SQL code also check. Use is null as in, if @date is null, to check if you were passed a null, if you need to write special case code to deal with that. The real issue is in your SP as far as I can see, you''re checking and passing in a null if your value is null, so your code in C# is fine.


这篇关于如何检查是否未分配DateTime变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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