Finding null value in Dataset - DataRow.IsNull method vs ==DbNull.Value - c#
问题
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}
回答1
There is no real practical benefit. Use whichever one seems more readable to you.
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).
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.
回答2
DBNull.Value != null
DBNull.Value stands for a column having the value <NULL>
. Pop open a table and return some rows, see if any column in any row contains the <NULL>
(ctrl 0) value. If you see one that is equivalent to DBNull.Value.
if you set a value to null or DBNull.Value then you will want to use IsNull()
. That returns true if the value was set to either null or DBNull.Value.
Consider the following:
row["myCol"] = null;
row["myCol"] = DBNull.Value
if (row["myCol"] == DBNull.Value)
//returns true
if (row["myCol"] == null)
//returns false
if (row.IsNull("myCol"))
//returns true
The point is if you are just checking for null or DBNull.Value use IsNull, if you are only checking for DBNull.Value explicitly say so and use that.
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"))
回答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
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...