DateTime.MinValue和SQLDATETIME溢出 [英] DateTime.MinValue and SqlDateTime overflow

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

问题描述

我不希望验证 txtBirthDate 所以我想通过 DateTime.MinValue 数据库。

I don't want to validate txtBirthDate so I want to pass DateTime.MinValue in database.

我的代码:

 if (txtBirthDate.Text == string.Empty)
    objinfo.BirthDate = DateTime.MinValue;
 else
     objinfo.BirthDate =  DateTime.Parse(txtBirthDate.Text);



DateTime.MinValue 收益 =日期{1/1/0001 12:00:00 AM}

我有一个SQL错误:

SQLDATETIME溢出。必须是1753年1月1日上午12时〇〇分00秒和12/31/9999下午11点59分59秒之间。

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

我站下,但我不明白为什么 DateTime.MinValue 返回无效的日期时间,这是无法在database.How插入来处理这种情况?

I under stand it but I don't understand why DateTime.MinValue return invalid date time which is unable to insert in database.How to handle this type of situation?

推荐答案

基本上不使用 DateTime.MinValue 来表示一个缺失值。您的不能的使用 DateTime.MinValue 在SQL Server DateTime字段,如SQL Server有1753年开始的最小值。

Basically, don't use DateTime.MinValue to represent a missing value. You can't use DateTime.MinValue in a SQL Server DateTime field, as SQL Server has a minimum value of the start of 1753.

相反,让你的出生日期属性可空< DateTime的> (又名的DateTime?),并将其设置为当你没有价值。另外,还要确保你的数据库字段为空。然后你只需要确保该在数据库中的 NULL 值结束。究竟如何做到这一点,将取决于你的数据访问,这你还没有告诉我们任何事情。

Instead, make your BirthDate property a Nullable<DateTime> (aka DateTime?), and set it to null when you don't have a value. Also make sure your database field is nullable. Then you just need to make sure that that null ends up as a NULL value in the database. Exactly how you do that will depend on your data access, which you haven't told us anything about.

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

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