简体   繁体   English

.Net SqlDataReader项为空[VB]

[英].Net SqlDataReader Item is Null [VB]

Ok, this is strange. 好吧,这很奇怪。 I ran into this issue tonight when I was adding a feature for our corporate site. 今晚在为公司网站添加功能时遇到了这个问题。

I'm building a custom calendar control that queries our database to display corporate events. 我正在构建一个自定义日历控件,该控件查询我们的数据库以显示公司事件。 Here is the situation, there is an EndDate value stored and on the dev system one of the events have a NULL value. 在这种情况下,存储了一个EndDate值,并且在开发系统上,其中一个事件具有NULL值。 No big deal since it's only a test system, but might as well check before trying to use it anyway on the safe side. 没什么大不了的,因为它只是一个测试系统,但是在尝试以安全的方式使用它之前最好还是进行检查。 I figured the following code would work: 我认为以下代码可以工作:

While dr.Read()
  corporateTable.Rows.Add(New Object() { _
    Convert.ToDateTime(dr("EventBeginDate")) _
    , IIf(dr("EventEndDate") Is DBNull.Value, Convert.ToDateTime(dr("EventBeginDate")).AddDays(1), Convert.ToDateTime(dr("EventEndDate"))) _
    , Convert.ToString(dr("EventType")) _
    , Convert.ToString(dr("EventDescription")) _
    , Convert.ToString(dr("EventMessage")) _
  })
End While

But it didn't, I still kept getting the Object cannot be cast from DBNULL error. 但事实并非如此,我仍然不断获取Object cannot be cast from DBNULL错误Object cannot be cast from DBNULL So I thought it over and came up with this code which works successfully, although I don't like it and think it's ugly. 因此,我考虑了一下,并提出了可以成功运行的代码,尽管我不喜欢它,并且认为它很丑陋。

While dr.Read()
  Dim column As Integer = 0
  While column < dr.FieldCount - 1
    If dr.GetName(column) = "EventEndDate" Then
      Exit While
    End If

    column += 1
  End While
  corporateTable.Rows.Add(New Object() { _
    Convert.ToDateTime(dr("EventBeginDate")) _
    , IIf(dr.IsDBNull(column), Convert.ToDateTime(dr("EventBeginDate")).AddDays(1), dr.Item(column)) _
    , Convert.ToString(dr("EventType")) _
    , Convert.ToString(dr("EventDescription")) _
    , Convert.ToString(dr("EventMessage")) _
  })
End While

The thing that really gets me is, at one point I had this: 真正让我着迷的是,我曾经有过这样的经历:

, IIf(dr.IsDBNull(column), Convert.ToDateTime(dr("EventBeginDate")).AddDays(1), Convert.ToDateTime(dr("EventEndDate"))) _

Thinking that it should work because it should only evaluate the dr() if it's not NULL . 认为它应该起作用是因为它仅应在不为NULL情况下评估dr() However, it kept erroring out at the end because the value was in fact NULL . 但是,由于该值实际上为NULL ,因此它始终在最后出错。

So to finally get to my question, sorry for the long explanation. 因此,终于解决了我的问题,对不起您的冗长解释。

Why is it that even though I'm checking if it's NULL before using the value, it errors out at the part that doesn't get called unless it's not NULL ? 为什么即使我在使用该值之前先检查它是否为NULL ,但除非它不是NULL否则它在不会被调用的部分会出错? Does it have to do with the fact that I'm using the IIF() and it's evaluating the whole statement? 它是否与我使用IIF()并评估整个语句有关? Or, using the dr() , it evaluates at runtime? 还是使用dr()在运行时求值?

I'm just stumped and would like to know what exactly is going on, so if it's possible, come up with a cleaner solution. 我只是很困惑,想知道到底发生了什么,因此,如果可能的话,提出一个更清洁的解决方案。

Thanks in advance! 提前致谢!

IIf always evaluates both the true and false parts - use inline If instead if you want to avoid this: IIf总是同时评估真假部分-如果要避免这种情况,请使用内联If代替:

While dr.Read()
  corporateTable.Rows.Add(New Object() { _
    Convert.ToDateTime(dr("EventBeginDate")) _
    , If(dr("EventEndDate") Is DBNull.Value, Convert.ToDateTime(dr("EventBeginDate")).AddDays(1), Convert.ToDateTime(dr("EventEndDate"))) _
    , Convert.ToString(dr("EventType")) _
    , Convert.ToString(dr("EventDescription")) _
    , Convert.ToString(dr("EventMessage")) _
  })
End While

Iif函数正在评估每个语句,对您的空检查使用不同的构造,例如IF函数

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

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