有关类型比较ASP.NET和DBNull的问题
题
我有一个从MSSQL数据库拉的文章记录功能。有些网址到PDF,和其他都存储在SQL实战篇。该存储不必在记录一个URL(为DBNull)的文章,所以我希望能够解析。我尝试一个简单的测试:
If Row.Item("url").GetType Is GetType(DBNull) Then
//' do something here....
End If
不过,我得到了 “Conversion from type 'DBNull' to type 'String' is not valid.
” 异常。有趣的是,当我的手表上的上述条件,则返回True
或False
。
任何人都知道为什么发生这种情况和/或方法来解决这个问题?谢谢!
解决方案
为什么不直接使用:
If Row.IsNull("url") Then
//' do something here....
End If
其他提示
我总是只使用这个测试上的记录:
If IsDBNull(strSQLTiggRes("url")) Then
'Do something .
Else
'do something else
end if
我喜欢
if (DBNull.Value.Equals(Row.Item("url")))
尝试
If Row.Item("url") = DBNull.Value
在错误是告诉你,Row.Item("url")
是System.String
所以在这一点的值不会由DbNull
。
尝试是这样的:
If Row.Item("url") Is Nothing Then
//' do something here....
End If
你就不能这样做。
If Row.Item("url") = DBNull.Value Then
End If
我的选择是:
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
使用此
if row("ColumnName") is DBNull.value Then
'do something
end if
您会发现,你必须使用在这种情况下,你的东东DTO比较值不只是类型。你也可以把它变成你可以用它来返回任何结果,而不是DBNull的可重复使用的功能
function checkvalue(item as object)
if item is dbnul.value then
return nothing
else
return item
end if
end function
不隶属于 StackOverflow