简体   繁体   中英

Which of IsDBNull and IsNull should be used?

If in VB.NET I have DataRow and I want to test whether a column value is Null , should I use:

myDataRow.IsNull("Column1")

OR

IsDBNull(myDataRow("Column1"))

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)

Here is what happens inside the IsNull call of DataRow :

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

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

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

Finally, here is what _storage.IsNull does:

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

Since dbNullBits is a BitArray , this operation completes very quickly.

Now consider what the indexer myDataRow("Column1") does (you call this indexer before passing its result to 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 IsNull method 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 DBNull or 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.

.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 very quickly. It should therefore be your preference.

I did bit of findings and found interesting facts which provides more insight on usage of DataRow.IsNull OR 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.

References: DataRow.IsNull and IsDBNull

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...

Most efficient way to check for dbnull...

Avoid checking for datarow isdbnull...

When dealing with DataRow data, it's ideal to use the IsDBNull() function. IsDBNull() has the advantage that it checks if your object represents a 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.

从数据库设计和使用的角度来看,IsNull是基于使用各种数据库技术(包括SQL,DB2,OLAP,MOLAP,RDBMS,MDBMS,SPSS,Essbase等)来查询列值的正确且可接受的方式。根据定义,Null是缺少值,未知,Null甚至不等于Null,因此Null的任何附件只是方便的问题。

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM