Bruce Wood said:
I think that John missed one important point.
While it is true that NULL in SQL is DBNull in .NET, and that DBNull is
an object, while null is a reference to nothing (and thus not an
object), it is _also_ true that DBNull and null in .NET arguably
represent the same concept: "There is nothing there."
Up until a few minutes ago I had been confounded by why the .NET
Framework designers decided that they needed a special class called
DBNull to represent something for which they already had a value: null.
In thinking about this post it came to me.
DBNull and null represent a fine distinction: that between "I did read
something from the database, and it says that this column in this row
contains nothing. NULL." and "There is nothing at this location. In the
context of my program, it is uninitialized." DBNull is a marker that
there is nothing at a certain location in the database; null is nothing
at all. In most programs the distinction is irrelevant. In fact, I
have methods coded into my applications that have no purpose other than
to mediate between DBNull and null: a DBNull read from the database is
transformed to a null in memory, and vice versa. I wonder if the fine
distinction between the two isn't more trouble than it's worth.
Bruce,
We're at risk of confusing the OP with subtleties which go beyond the
initial question. However, there are some response I want to make.
First, null (or Nothing) in .NET is what a reference type contains when it
doesn't reference anything. It cannot be used for value types. On the other
hand, an Integer database column could return NULL (DBNull.Value) indicating
that there is no value there, integer or otherwise.
To get more specific about SQL and NULL, consider the following query:
SELECT L.ID, L.A, R.B
FROM LeftTable L LEFT OUTER JOIN RightTable R
ON L.ID = R.LID
Such a query can return things like:
L.ID L.A R.B
1 2 3
2 3 4
3 4 NULL
In this case, the NULL simply means that there is no row in table RightTable
which has LID set to 3, so there is no answer to the question "what is the
value of R.B for this row of the query". This doesn't mean that there is
some row in RightTable that has a NULL in the B column, it means that this
query has no answer to "R.B" in this row.
Recalling what I learned in college about Relational Database Theory, if a
NULLable column of a table has no value, that is exactly equivalent to the
LEFT OUTER JOIN situation, as the theory isn't about tables at all, but is
about tuples, and the tuples you get from a table could as easily have been
generated from some join, and the mathematics still work.
John Saunders
P.S. Past this point, you need an expert in relational database theory,
which I am not.