数据列的DbNull.Value和isDBNull

时间:2016-07-11 10:03:37

标签: asp.net vb.net optimization dbnull

我有DataRow,其中包含database的数据。我想在Null条件中检查每个数据列的IF值。 我发现了两种检查NULL值的方法。

 If IsDBNull(drType("ISShort")) Then
    StartDate.Visible = True
 Else
    StartDate.Visible = False
 End If

 If Not drType("ISShort").ToString Is DBNull.Value  Then
   StartDate.Visible = True
 Else
   StartDate.Visible = False
 End If

两者都适合我,但我不知道哪一个更好用?

2 个答案:

答案 0 :(得分:3)

我更喜欢DataRow.IsNull,它返回bool,可读且高效:

StartDate.Visible = drType.IsNull("ISShort")

相关:Which of IsDBNull and IsNull should be used?

请注意,您的第二种方法并不奏效。如果您使用String将其转换为ToString,则无法DBNull.Value。只编译选项严格设置为off,我强烈反对。

答案 1 :(得分:1)

第二种情况没有意义,因为它没有必要ToString()

注意,还有另一种方法可以使用DbNull

If DbNull.Value.Equals(row.Item(fieldName)) Then
...

你也可以使用myDataRow.IsNull(fieldName),根据Which of IsDBNull and IsNull should be used?

更快
相关问题