Which of IsDBNull and IsNull should be used?

后端 未结 7 1973
傲寒
傲寒 2021-01-07 23:02

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

myDataRow.IsNull(\"Column         


        
相关标签:
7条回答
  • 2021-01-07 23:13

    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.

    0 讨论(0)
  • 2021-01-07 23:27

    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.

    0 讨论(0)
  • 2021-01-07 23:32

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

    0 讨论(0)
  • 2021-01-07 23:33

    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.

    0 讨论(0)
  • 2021-01-07 23:36

    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.

    0 讨论(0)
  • 2021-01-07 23:38

    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.

    0 讨论(0)
提交回复
热议问题