关于 ASP.NET 和 DBNull 类型比较的问题

发布于 2024-07-21 08:16:02 字数 380 浏览 6 评论 0原文

我有一个从 MSSQL 数据库中提取文章记录的函数。 有些是 PDF 的 URL,有些是存储在 SQL 中的实际文章。 存储的文章在记录中没有 URL (DBNull),因此我希望能够解析它。 我尝试了一个简单的测试:

If Row.Item("url").GetType Is GetType(DBNull) Then
    //' do something here....
End If

但是,我收到“从类型“DBNull”到类型“String”的转换无效。”异常。 有趣的是,当我对上述条件进行监视时,它返回 True 或 False。

有人知道为什么会发生这种情况和/或解决这个问题的方法吗? 谢谢!

I have a function that pulls articles records from an MSSQL database. Some are URLs to PDFs, and other are actual articles stored in the SQL. The articles that are stored do not have a URL (DBNull) in the record, so I want to be able to parse that. I tried a simple test:

If Row.Item("url").GetType Is GetType(DBNull) Then
    //' do something here....
End If

However, I get the "Conversion from type 'DBNull' to type 'String' is not valid." exception. The funny part is, when I do a watch on the above conditional, it returns True or False.

Anyone know why this is happening and/or a way to fix this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

栖迟 2024-07-28 08:16:03

我总是记录下来使用这个测试:

If IsDBNull(strSQLTiggRes("url")) Then
     'Do something                   .
Else
     'do something else
end if

I always just use this test on the record:

If IsDBNull(strSQLTiggRes("url")) Then
     'Do something                   .
Else
     'do something else
end if
顾铮苏瑾 2024-07-28 08:16:03

为什么不直接使用:

If Row.IsNull("url") Then
    //' do something here....
End If

Why not just use:

If Row.IsNull("url") Then
    //' do something here....
End If
撩动你心 2024-07-28 08:16:03

我喜欢

if (DBNull.Value.Equals(Row.Item("url")))

I like

if (DBNull.Value.Equals(Row.Item("url")))
尘曦 2024-07-28 08:16:03

尝试

If Row.Item("url") = DBNull.Value

Try

If Row.Item("url") = DBNull.Value
娇纵 2024-07-28 08:16:03

该错误告诉您 Row.Item("url") 是一个 System.String,因此此时的值不会是 DbNull

尝试这样的事情:

If Row.Item("url") Is Nothing Then
    //' do something here....
End If

The error is telling you that Row.Item("url") is a System.String so the value at this point will not by DbNull.

Try something like this:

If Row.Item("url") Is Nothing Then
    //' do something here....
End If
生死何惧 2024-07-28 08:16:03

你不能这样做吗

If Row.Item("url") = DBNull.Value Then

End If

Can't you just do

If Row.Item("url") = DBNull.Value Then

End If
数理化全能战士 2024-07-28 08:16:03

我的偏好是:

If Not Object.Equals(DBNull.Value, Row.Item("url")) Then
  'Hooray!
End If

在 VB 中,您还可以直接使用 IsDBNull 函数(Microsoft.VisualBasic 免责声明):

If Not IsDBNull(Row.Item("url")) Then
  'Hooray!
End It

My preference is:

If Not Object.Equals(DBNull.Value, Row.Item("url")) Then
  'Hooray!
End If

In VB, you can also use the IsDBNull function (Microsoft.VisualBasic disclaimer) directly :

If Not IsDBNull(Row.Item("url")) Then
  'Hooray!
End It
我喜欢麦丽素 2024-07-28 08:16:03

用这个。

if row("ColumnName") is DBNull.value Then
     'do something
end if

您会发现在这种情况下必须使用 is 并且需要比较值而不仅仅是类型。 您可能还想将其放入可重用函数中,您可以使用它来返回任何内容而不是 dbnull

function checkvalue(item as object)
     if item is dbnul.value then
          return nothing
     else
          return item
     end if
 end function

Use this.

if row("ColumnName") is DBNull.value Then
     'do something
end if

You will find that you must use is in this case and you nee dto compare the values not just the types. You may also want to put it into a reusable function that you can use to return nothing instead of dbnull

function checkvalue(item as object)
     if item is dbnul.value then
          return nothing
     else
          return item
     end if
 end function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文