问题
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"))
回答1:
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.
回答2:
.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.
回答3:
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...
回答4:
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.
回答5:
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.
回答6:
I would always use myDataRow.IsNull("Column1")
because when issuing a SELECT
, if the value is null
it's returned as null
and not DBNull
.
回答7:
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.
来源:https://stackoverflow.com/questions/17548023/which-of-isdbnull-and-isnull-should-be-used