What Is DBNull Needed For Anyway? [duplicate]
Possible Duplicate:
What is the point of DBNull?
Since the beginning of my adventure with .NET I have always asked myself one question:
why do we need the DBNull type? Is a simple null reference not enough?
MSDN says that DBNull "Represents a nonexistent value". From a logical point of view - this one sentence explains why a null reference cannot be used - because it is a null reference and not a lack of value. But is it enough to introduce a type that causes a lot of trouble?
BTW: If anyone has something to say in defense of th开发者_开发技巧e DBNull
I would really appreciate it.
DBNull has existed since the earliest versions of .Net framework.
Nullable value types (Nullable<T>
) has only existed since version 2 of the framework.
Since, before version 2, it was possible to receive, say, a null int value back from the database, how would you propose it be represented? Then, for consistency, the same was used to represent all DB nulls, whether they translated to a value or reference type.
DBNull.Value
represents 'NULL' in the database; it is not the same as 'null'.
We need it so we can detect if a value returned from a database is null or not.
Typically, Convert.IsDBNull
is used to perform the check.
The DBNull
type is not the same as the programming value null
null in c# is something different than NULL in the SQL context, the SQL NULL in C# is exposed not as string but as DbNull.Value
which makes sense, if it was not like that how would you check for db null? comparing a string "NULL"
would not have been the same...
精彩评论