日期转换设置为零 [英] date convertions is set to zero

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

问题描述

未将日期转换为"0"

not converted date to "0"

if (txtDate.Text.ToString() == "")
               txtDate.Text = "0";

推荐答案

如果必须在日期字段中使用NULL,则可以这样..

在UPDATE QUERY或INSERT QUERY SORT ANY SQL QUERY中,您可以将"case when"插入或更新日期值为空白.

这样.

If you have to allow NULL to your date field you can go like this..

in UPDATE QUERY OR INSERT QUERY IN SORT ANY SQL QUERY you can put "case when" for inserting or updating date value to blank.

like this.

UPDATE TABLENAME SET ENTRYDATE = CASE WHEN @DATE = '' THEN NULL ELSE @DATE END



或进行插入查询



OR FOR INSERT QUERY

INSERT INTO TABLENAME (USERNAME,ENTRYDATE) VALUES ('TEJASVAISHNAV',CASE WHEN @DATE = '' THEN NULL ELSE @DATE END)


您好,

当您说空"时,我想您的意思是空白,根本没有日期或使用适当的术语-null?如果要向数据库提交null日期值,请确保您的数据列允许为空,然后将该值设置为null.请勿尝试传递空字符串-这将无法工作-除非您使用varchar或nchar字段来填充adtes,否则无论如何都是不好的做法...

没有您的代码,这将非常困难,但是您的代码应如下所示:
Hi there,

When you say "empty" I presume you mean blank, no date at all, or in proper terms - null? If you want to submit a null date value to your databse, make sure your data column allows nulls then set the value to null. Do no try to pass in empty strings - this wont work - unless you are using a varchar or nchar field for stroing adtes which is bad practice anyway...

Without your code this is very hard but your code should look something like this:
DateTime? DateToSubmit = null;
if(!string.IsNullOrEmpty(txtDate.text))
{
    DateTime.TryParse(txtDate.Text, out DateToSubmit);
}
//Submit DateToSubmit to your database
//If field is DateTime
MyDataObject.MyDate = DateToSubmit;
//or if field is string
MyDataObject.MyDate = DateToSubmit.HasValue ? DateToSubmit.Value.ToString() : "";



希望这会有所帮助,
Ed



Hope this helps,
Ed


如果您的表接受NULL值,请将其设置为DBNULL.Value.
如果不是,请设置默认日期,例如''1900-01-01''.
If your table accept NULL value, set it DBNULL.Value.
If not, set default date, like ''1900-01-01''.


这篇关于日期转换设置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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