在数据集中查找空值 - DataRow.IsNull方法vs == DbNull.Value - c# [英] Finding null value in Dataset - DataRow.IsNull method vs ==DbNull.Value - c#
问题描述
使用c#方法DataRow.IsNull通过检查行是否等于DbNull.value来确定空值有什么好处?
What are the benefits of using the c# method DataRow.IsNull to determine a null value over checking if the row equals DbNull.value?
if(ds.Tables[0].Rows[0].IsNull("ROWNAME")) {do stuff}
vs
if(ds.Tables[0].Rows[0]["ROWNAME"] == DbNull.value) {do stuff}
推荐答案
没有真正的实用益处。
There is no real practical benefit. Use whichever one seems more readable to you.
对于它们之间的特殊区别,基本的答案是 IsNull
查询列中特定记录的空状态。使用 == DBNull.Value
实际上检索值,并在实际上为null的情况下进行替换。换句话说, IsNull
检查状态而不实际检索值,因此稍微更快一些(理论上至少)。
As to the particular differences between them, the basic answer is that IsNull
queries the null state for a particular record within a column. Using == DBNull.Value
actually retrieves the value and does substitution in the case that it's actually null. In other words, IsNull
checks the state without actually retrieving the value, and thus is slightly faster (in theory, at least).
理论上,如果您要使用自定义属性,列返回 other 而不是 DBNull.Value
存储类型,但这从来没有做过(在我的经验)。如果是这种情况, IsNull
将处理存储类型使用除 DBNull.Value
之外的其他情况的情况, ,再次,我从来没有见过这样做。
It's theoretically possible for a column to return something other than DBNull.Value
for a null value if you were to use a custom storage type, but this is never done (in my experience). If this were the case, IsNull
would handle the case where the storage type used something other than DBNull.Value
, but, again, I've never seen this done.
这篇关于在数据集中查找空值 - DataRow.IsNull方法vs == DbNull.Value - c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!