日期时间问题与01/01/1900 [英] datetime issue with 01/01/1900

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

问题描述

我在sql server中有一个datetime列及其可选字段,如果用户决定不输入,那么我想在表中插入NULL作为NULL,我定义如下:

  @deadlineDate datetime = null 

当我插入到sql server我有这个代码在asp.net

  private DateTime? GetDeadlineDate()
{
DateTime? getDeadlineDate = null;
if(!string.IsNullOrEmpty(DeadlineDate.SelectedDate))
{
getDeadlineDate = DateTime.Parse(DeadlineDate.SelectedDate).Date;
}
if(!getDeadlineDate.HasValue)
{
return null;
}
return getDeadlineDate.Value;

}

但问题是:插入

  1900-01-01 00:00:00.000 

在sql表而不是 NULL



我在这里做错什么?



更新:

  private DateTime? GetDeadlineDate()
{
DateTime? getDeadlineDate = null;
if(!string.IsNullOrEmpty(DeadlineDate.SelectedDate))
{
getDeadlineDate = DateTime.Parse(DeadlineDate.SelectedDate).Date;
}
if(!getDeadlineDate.HasValue)
{
return DBNull.Value; // throws error ....
}
return getDeadlineDate.Value;
}


解决方案

假设你有: p>

  DateTime? date = GetDate(); 
command.Parameters.Add(@ date)。Value = date;

如果 date == null 想要插入SQL NULL即 DBNull.Value ,所以你应该做下一步:

  DateTime? date = GetDate(); 
command.Parameters.Add(@ date)。Value =(object)date? DBNull.Value;

这意味着相同:

  if(date!= null)
//使用日期
else
//使用DBNull.Value

如果您想要关注函数中的可空的日期时间,那么您应该再次声明:

 私有对象GetDate()
{
DateTime date;
返回DateTime.TryParse(selectedDate,out date)? date:DBNull.Value;
}

command.Parameters.Add(@ date)。Value = GetDate();

但我不建议这样做,然后使用下一个:

  command.Parameters.Add(@ date)。Value =(object)GetDate()? DBNull.Value; 


i have a datetime column in sql server and its optional field and if the user decided not to enter then i want to insert the value as NULL in the table and i define something like this:

@deadlineDate datetime = null

when i am inserting into sql server i have this code in asp.net

private DateTime? GetDeadlineDate()
{
    DateTime? getDeadlineDate = null;
    if (!string.IsNullOrEmpty(DeadlineDate.SelectedDate))
    {
       getDeadlineDate = DateTime.Parse(DeadlineDate.SelectedDate).Date;
    }
    if (!getDeadlineDate.HasValue)
    {
        return null;
    }
    return getDeadlineDate.Value;

}

but the problem is: its inserting

1900-01-01 00:00:00.000

in the sql table instead of NULL

what i am doing wrong here?

UPDATE:

private DateTime? GetDeadlineDate()
{
    DateTime? getDeadlineDate = null;
    if (!string.IsNullOrEmpty(DeadlineDate.SelectedDate))
    {
       getDeadlineDate = DateTime.Parse(DeadlineDate.SelectedDate).Date;
    }
    if (!getDeadlineDate.HasValue)
    {
        return DBNull.Value; //throws error....
    }
    return getDeadlineDate.Value;          
}

解决方案

Assuming you have:

DateTime? date = GetDate();
command.Parameters.Add("@date").Value = date;

in case when date == null you want to insert SQL NULL i.e. DBNull.Value so you should do next:

DateTime? date = GetDate();
command.Parameters.Add("@date").Value = (object)date ?? DBNull.Value;

which means the same as:

if(date != null)
     // use date
else
     // use DBNull.Value

if you want to take care about nullable datetime in your function you should declare it next way:

private object GetDate()
{
    DateTime date;
    return DateTime.TryParse(selectedDate, out date) ? date : DBNull.Value;
}

command.Parameters.Add("@date").Value = GetDate();

but I don't recommend to do that and use next:

command.Parameters.Add("@date").Value = (object)GetDate() ?? DBNull.Value;

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

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