在数据集中查找空值 - DataRow.IsNull 方法 vs ==DbNull.Value - c# [英] Finding null value in Dataset - DataRow.IsNull method vs ==DbNull.Value - c#

查看:14
本文介绍了在数据集中查找空值 - DataRow.IsNull 方法 vs ==DbNull.Value - c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与检查行是否等于 DbNull.value 相比,使用 c# 方法 DataRow.IsNull 确定空值有什么好处?

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}

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 实际上检索该值并在它实际上为空的情况下进行替换.换句话说,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆