检查DBNull会引发StrongTypingException [英] Checking for DBNull throws a StrongTypingException
问题描述
我正在使用数据集从数据库中提取数据。一行中的一个字段是 NULL
。我知道这个。但是,以下vb.net代码抛出 StrongTypingException
(在数据集设计器的自动生成的get_SomeField()方法中):
I am using a dataset to pull data from a DB. One of the fields in a row is NULL
. I know this. However, the following vb.net code throws a StrongTypingException
(in the autogenerated get_SomeField() method in the dataset designer):
If Not IsDBNull(aRow.SomeField) Then
'do something
End If
根据文档和这个问题应该没问题。
According to documentation and this question it should be fine.
编辑:如果aRow.SomeField是DBNull.Value然后
也返回相同的错误。嗯。
edit: If aRow.SomeField is DBNull.Value Then
also returns the same error. Argh.
推荐答案
不同之处在于,在相关问题中,它所指的是无类型值(即对象
)。当您通过 .SomeField
进行访问时,该类型已经包含在内-因此可以是 int
等。尝试在 int
上尝试 IsDBNull
作为 int
不能从不是 DBNull
。
The difference is that in the related question it is talking about an untyped value (i.e. object
) via an indexer. When you go via .SomeField
, the type is already included - so this could be int
etc. And it wouldn't make sense to try IsDBNull
on an int
, as an int
can never be DBNull
.
基本上是 SomeField
是(例如C#口音...)的包装。
Essentially the SomeField
is a wrapper for (excuse the C# accent...)
public int SomeField {
get { return (int) this["someFieldName"]; }
set { this["someFieldName"] = value; }
}
我不是一个庞大的 DataTable
人,但是您可以尝试按名称/索引/列进行检查;或将列标记为可为空,以使其为 Nullable< int>
(在上面的示例中)。
I'm not a huge DataTable
person, but you could try checking it by name/index/column; or marking the column as nullable so that it is Nullable<int>
(in the example above).
这篇关于检查DBNull会引发StrongTypingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!