.net 应该使用 IsDBNull 和 IsNull 中的哪一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17548023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:21:24  来源:igfitidea点击:

Which of IsDBNull and IsNull should be used?

.netvb.netnulldbnull

提问by CJ7

If in VB.NETI have DataRowand I want to test whether a column value is Null, should I use:

如果在VB.NET我有DataRow并且我想测试列值是否为Null,我应该使用:

myDataRow.IsNull("Column1")

OR

或者

IsDBNull(myDataRow("Column1"))

回答by dasblinkenlight

Short answer: use the first way, it is faster, because the first method uses a pre-computed result, while the second method needs to recompute it on the fly every time you call it.

简短回答:使用第一种方法,它更快,因为第一种方法使用预先计算的结果,而第二种方法需要在每次调用时动态重新计算它。

Long answer: (you need to read C# code to understand this part; MS supplies framework code in C#, but VB programmers should be able to get the general idea of what's going on)

长答案:(你需要阅读 C# 代码才能理解这部分;MS 在 C# 中提供框架代码,但 VB 程序员应该能够大致了解正在发生的事情)

Here is what happens inside the IsNullcall of DataRow:

这是在IsNull调用内部发生的事情DataRow

public bool IsNull(string columnName) {
    DataColumn column = GetDataColumn(columnName);
    int record = GetDefaultRecord();
    return column.IsNull(record);
}

The column.IsNullperforms a quick assertion, and forwards the call to DataStorage, an internal class:

column.IsNull执行快速断言,并转发呼叫DataStorage,一个内部类:

internal bool IsNull(int record) {
    Debug.Assert(null != _storage, "no storage");
    return _storage.IsNull(record);
}

Finally, here is what _storage.IsNulldoes:

最后,这是什么_storage.IsNull

public virtual bool IsNull(int recordNo) {
    return this.dbNullBits.Get(recordNo);
}

Since dbNullBitsis a BitArray, this operation completes very quickly.

由于dbNullBits是 a BitArray,此操作完成得非常快。

Now consider what the indexer myDataRow("Column1")does (you call this indexer before passing its result to IsDBNull):

现在考虑索引器的myDataRow("Column1")作用(在将其结果传递给 之前调用此索引器IsDBNull):

get {
    DataColumn column = GetDataColumn(columnName);
    int record = GetDefaultRecord();
    _table.recordManager.VerifyRecord(record, this);
    VerifyValueFromStorage(column, DataRowVersion.Default, column[record]);
    return column[record];
}

Note that the first two lines of IsNullmethod and the indexer are identical. However, the three rows that follow need to perform validation, and fetch the value itself. Only after that your code can start computing its target value - a flag that tells it if the value is DBNullor not. This requires more computation, but more importantly, it requires some computation every time you perform the check. This is slower than using a pre-computed value.

请注意,方法的前两行IsNull和索引器是相同的。但是,后面的三行需要执行验证,并获取值本身。只有在那之后,您的代码才能开始计算其目标值 - 一个标志,告诉它该值是否DBNull存在。这需要更多的计算,但更重要的是,每次执行检查时都需要进行一些计算。这比使用预先计算的值要慢。

回答by Hans Passant

.NET almost never gives you two ways to do the same thing by accident. DataRow.IsNull() is more efficient, it avoids having to retrieve the column value and then checking for IsDBNull. Internally it already keeps track of whether a column is null, determined when the row was created. So IsNull() can give you that veryquickly. It should therefore be your preference.

.NET 几乎从来没有给你两种偶然地做同一件事的方法。DataRow.IsNull() 更有效,它避免了必须检索列值然后检查 IsDBNull。在内部,它已经跟踪列是否为空,确定行的创建时间。所以ISNULL()可以给你非常快。因此,这应该是您的偏好。

回答by Twinkle

I did bit of findings and found interesting facts which provides more insight on usage of DataRow.IsNullOR IsDBNull.

我做了一些发现并发现了有趣的事实,这些事实提供了对DataRow.IsNullOR使用的更多见解IsDBNull

DataRow.IsNull - Gets a value that indicates whether the specified DataColumn contains a null value. Convert.IsDBNull - Returns an indication whether the specified object is of type DBNull.

DataRow.IsNull - 获取一个值,该值指示指定的 DataColumn 是否包含空值。Convert.IsDBNull - 返回指定对象是否为 DBNull 类型的指示。

References: DataRow.IsNulland IsDBNull

参考资料:DataRow.IsNullIsDBNull

The most interesting discussion which provides clear conclusion can be drawn from Performance Consideration

提供明确结论的最有趣的讨论可以从性能考虑中得出

Nearly similar discussion were done earlier, here are references:

之前做过几乎类似的讨论,这里是参考:

Finding null value in dataset datarow isnull...

在数据集数据行中查找空值 isnull...

Most efficient way to check for dbnull...

检查 dbnull 的最有效方法...

Avoid checking for datarow isdbnull...

避免检查数据行 isdbnull ...

回答by Peter Birdsall

From a database design and usage point of view, IsNull is the correct and accepted way to interrogate the value of a column, based on use of various database technologies including SQL, DB2, OLAP, MOLAP, RDBMS, MDBMS, SPSS, Essbase, etc. By definition Null is the absence of a value, a unknown, and Null is not even equal to Null, so any adjunct to Null is just a matter of convenience.

从数据库设计和使用的角度来看,IsNull 是查询列值的正确且可接受的方式,基于使用各种数据库技术,包括 SQL、DB2、OLAP、MOLAP、RDBMS、MDBMS、SPSS、Essbase 等. 根据定义,Null 是没有值,未知数,而 Null 甚至不等于 Null,因此 Null 的任何附加项都只是为了方便。

回答by Mark Micallef

When dealing with DataRow data, it's ideal to use the IsDBNull() function. IsDBNull() has the advantage that it checks if your object representsa null, rather than simply being null itself, and that's an important difference. When you are interrogating a data row item which is null in the database, the item itself exists as an object, but it represents a NULL value. If you use IsNull() you will miss NULL values.

在处理 DataRow 数据时,最好使用 IsDBNull() 函数。IsDBNull() 的优点是它检查您的对象是否表示空值,而不是简单地为空值本身,这是一个重要的区别。当您查询数据库中为空的数据行项目时,该项目本身作为一个对象存在,但它代表一个 NULL 值。如果您使用 IsNull(),您将错过 NULL 值。

回答by Mike Perrenoud

I would always use myDataRow.IsNull("Column1")because when issuing a SELECT, if the value is nullit's returned as nulland not DBNull.

我总是会使用,myDataRow.IsNull("Column1")因为在发出 a 时SELECT,如果值为 ,null则返回 asnull而不是DBNull

回答by Tapas Mahata

Use IsDBNullIsNull is a little different from IsDBNull. One checks for a DB Null value from the database used and the other, well, just Null.

使用IsDBNullIsNull 与 IsDBNull 略有不同。一个检查所用数据库中的 DB Null 值,另一个检查是否为 Null。