Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 测试空值和数字的日期值_C# - Fatal编程技术网

C# 测试空值和数字的日期值

C# 测试空值和数字的日期值,c#,C#,我正在逐行读取excel文件,并将项目添加到集合中 将保存到数据库中 在数据库中: NumberValue1 to NumberValue3 are numbers and nullable. Datevalue1 to Datavalue5 are dates and nullable. BooleanYN1 is a varchar2(1 char) and nullable. 我希望能够测试这些数字、字符串和日期值,这样我就不会在数据库中插入null 我该怎么办?对于低于测试的字符串,

我正在逐行读取excel文件,并将项目添加到集合中 将保存到数据库中

在数据库中:

NumberValue1 to NumberValue3  are numbers and nullable.
Datevalue1 to Datavalue5 are dates and nullable.
BooleanYN1 is a varchar2(1 char) and nullable.
我希望能够测试这些数字、字符串和日期值,这样我就不会在数据库中插入null

我该怎么办?对于低于测试的字符串,应该可以。 我特别注意日期变量和数字

if ((col2Value != null && col3Value != null & col4Value != null))
{
    excelFileDataList.Add(new ExcelData 
    {
        BusinessUnitCode = col2Value.ToString(),
        GenericJobId = Convert.ToInt32(col3Value),

        NumberValue1 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col8Value),
        NumberValue2 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col9Value),
        NumberValue3 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col10Value),

        StringValue1 = col18Value == null ? "" : col18Value.ToString(),
        StringValue2 = col19Value == null ? "" : col19Value.ToString(),
        StringValue3 = col20Value == null ? "" : col20Value.ToString(),

        DateValue1 = Convert.ToDateTime(col28Value) == null ?  : Convert.ToDateTime(col28Value),
        DateValue2 = Convert.ToDateTime(col29Value) == null ?  : Convert.ToDateTime(col29Value),
        DateValue3 = Convert.ToDateTime(col30Value) == null ?  : Convert.ToDateTime(col30Value),
        DateValue4 = Convert.ToDateTime(col31Value) == null ?  : Convert.ToDateTime(col31Value),
        DateValue5 = Convert.ToDateTime(col32Value) == null ?  : Convert.ToDateTime(col32Value),

        BooleanYN1 = col34Value == null ? "" : col34Value.ToString(),
        BooleanYN2 = col35Value == null ? "" : col35Value.ToString(),
        BooleanYN3 = col36Value == null ? "" : col36Value.ToString(),                                            
    });

我一直在获取未设置为对象实例的对象引用。我认为这是空值的结果。excel电子表格中的各个列都有空值,这是可以接受的

在调用对象上的
Convert.ToInt32
Convert.ToDateTime
之前,您需要测试空值(因为传入空值将引发异常)。而不是:

NumberValue1 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col8Value)
DateValue1 = Convert.ToDateTime(col28Value) == null ?  : Convert.ToDateTime(col28Value)
您将需要:

NumberValue1 = col8Value == null ? 0 : Convert.ToInt32(col8Value)
DateValue1 = col28Value == null ? DateTime.MinValue : Convert.ToDateTime(col28Value)
而不是:

NumberValue1 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col8Value)
DateValue1 = Convert.ToDateTime(col28Value) == null ?  : Convert.ToDateTime(col28Value)
您将需要:

NumberValue1 = col8Value == null ? 0 : Convert.ToInt32(col8Value)
DateValue1 = col28Value == null ? DateTime.MinValue : Convert.ToDateTime(col28Value)
或者,如果类支持可为空的
值:

DateValue1 = col28Value == null ? null : (DateTime?)Convert.ToDateTime(col28Value)

在调用对象上的
Convert.ToInt32
Convert.ToDateTime
之前,需要测试null(因为传入null值将引发异常)。而不是:

NumberValue1 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col8Value)
DateValue1 = Convert.ToDateTime(col28Value) == null ?  : Convert.ToDateTime(col28Value)
您将需要:

NumberValue1 = col8Value == null ? 0 : Convert.ToInt32(col8Value)
DateValue1 = col28Value == null ? DateTime.MinValue : Convert.ToDateTime(col28Value)
而不是:

NumberValue1 = Convert.ToInt32(col8Value) == null ? 0 : Convert.ToInt32(col8Value)
DateValue1 = Convert.ToDateTime(col28Value) == null ?  : Convert.ToDateTime(col28Value)
您将需要:

NumberValue1 = col8Value == null ? 0 : Convert.ToInt32(col8Value)
DateValue1 = col28Value == null ? DateTime.MinValue : Convert.ToDateTime(col28Value)
或者,如果类支持可为空的
值:

DateValue1 = col28Value == null ? null : (DateTime?)Convert.ToDateTime(col28Value)

对于您的数字和日期,我建议您使用.TryParse()


对于您的数字和日期,我建议您使用.TryParse()


谢谢。那很有帮助,非常感谢。这很有帮助。