简体   繁体   English

c# 脚本内可为空的 DateTime

[英]c# nullable DateTime within script

I have written a little bit of code, but while trying to add a date with "null" as a resultset from a database, I get an error, that null is not date / time.我已经编写了一些代码,但是在尝试从数据库中添加带有“null”的日期作为结果集时,我收到一个错误,即 null 不是日期/时间。

So it seems, I have an error with the following code snippet... My question is: how to set the datatype correctly, that a "null" dataset would not create an error?所以看来,我对以下代码片段有一个错误......我的问题是:如何正确设置数据类型,“空”数据集不会产生错误?

As you can see, first of all I make a difference between some datatypes to create the right objects.如您所见,首先我在一些数据类型之间做出区别以创建正确的对象。 But in this one case, for the date, I want to allow null objects.但在这种情况下,对于日期,我想允许 null 对象。

Could you please help me?请你帮助我好吗?

case kdType.Numeric20:
nKSI = ktSI.CreateKeyword(Int32.Parse(""+row[column]));
break;
case kdType.AlphaNumeric:
nKSI = ktSI.CreateKeyword(""+row[column]);
break;
case kdType.Date:                           
nKSI = ktSI.CreateKeyword(DateTime.ParseExact(""+row[column], "dd.MM.yyyy HH:mm:ss", null));                
break;
case kdType.Currency:
nKSI = ktSI.CreateKeyword(Convert.ToDecimal(float.Parse(""+row[column], CultureInfo.CurrentCulture.NumberFormat)));
break;
case kdType.SpecificCurrency:
nKSI = ktSI.CreateKeyword(Convert.ToDecimal(float.Parse(""+row[column], CultureInfo.CurrentCulture.NumberFormat)));
break;

You have to chek for db null value before trying to use DateTime.ParseExact在尝试使用 DateTime.ParseExact 之前,您必须检查 db null 值

something like this像这样的东西

object dbValue = row[column]
if (dbValue == DbNull.Value)
{
//handle NULL somehow
}
else
{
.... =DateTime.ParseExact(dbValue.ToString(), ....);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM