Is there any difference between null and System.DBNull.Value? If yes, what is it?
null和System.DBNull.Value之间有什么区别吗?如果是,那是什么?
I noticed this behavior now -
我注意到了这种行为
while (rdr.Read())
{
if (rdr["Id"] != null) //if (rdr["Id"] != System.DBNull.Value)
{
int x = Convert.ToInt32(rdr["Id"]);
}
}
While I retrieve data from the database using a sql datareader, though there is no value returned if(rdr["Id"] != null)
returned true
and eventually threw an exception for casting a null as integer.
虽然我使用sql datareader从数据库检索数据,但是如果(rdr["Id"] != null)返回true并最终抛出一个将null作为整数的异常,则没有返回值。
But, this if I use if (rdr["Id"] != System.DBNull.Value)
returns false
.
但是,如果我使用if (rdr["Id"] != System.DBNull.Value)返回false。
What's the difference between null and System.DBNull.Value?
null和System.DBNull.Value之间有什么区别?
6 个解决方案
#1
94
Well, null
is not an instance of any type. Rather, it is an invalid reference.
null不是任何类型的实例。相反,它是一个无效的引用。
However, System.DbNull.Value
, is a valid reference to an instance of System.DbNull
(System.DbNull
is a singleton and System.DbNull.Value
gives you a reference to the single instance of that class) that represents nonexistent* values in the database.
然而,System.DbNull。值,是对系统实例的有效引用。DbNull(系统。DbNull是一个单例和System.DbNull。值为您提供对该类的单个实例的引用),该实例表示数据库中不存在的*值。
*We would normally say null
, but I don't want to confound the issue.
我们通常会说null,但我不想混淆这个问题。
So, there's a big conceptual difference between the two. The keyword null
represents an invalid reference. The class System.DbNull
represents a nonexistent value in a database field. In general, we should try avoid using the same thing (in this case null
) to represent two very different concepts (in this case an invalid reference versus a nonexistent value in a database field).
所以,这两者在概念上有很大的不同。关键字null表示无效引用。类系统。DbNull表示数据库字段中不存在的值。通常,我们应该尽量避免使用相同的东西(在本例中为null)来表示两个非常不同的概念(在本例中是无效引用,而在数据库字段中是不存在的值)。
Keep in mind, this is why a lot of people advocate using the null object pattern in general, which is exactly what System.DbNull
is an example of.
记住,这就是为什么很多人提倡使用空对象模式,这正是系统。DbNull就是一个例子。
#2
20
From the documentation of the DBNull class:
来自DBNull类的文档:
Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.
不要将面向对象编程语言中的null概念与DBNull对象混淆。在面向对象的编程语言中,null意味着没有对对象的引用。DBNull表示未初始化的变量或不存在的数据库列。
#3
9
DBNull.Value is annoying to have to deal with.
DBNull。要处理价值是很烦人的。
I use static methods that check if it's DBNull and then return the value.
我使用静态方法检查它是否为DBNull,然后返回值。
SqlDataReader r = ...;
String firstName = getString(r[COL_Firstname]);
private static String getString(Object o) {
if (o == DBNull.Value) return null;
return (String) o;
}
Also, when inserting values into a DataRow, you can't use "null", you have to use DBNull.Value.
此外,在将值插入DataRow时,不能使用“null”,必须使用DBNull.Value。
Have two representations of "null" is a bad design for no apparent benefit.
有两个“空”的表示是一个糟糕的设计,没有明显的好处。
#4
5
DBNull.Value is what the .NET Database providers return to represent a null entry in the database. DBNull.Value is not null and comparissons to null for column values retrieved from a database row will not work, you should always compare to DBNull.Value.
DBNull。值是. net数据库提供者返回来表示数据库中的空条目的值。DBNull。值不为空,对于从数据库行检索的列值,比较为空将不起作用,您应该始终与DBNull.Value进行比较。
http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx
http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx
#5
3
DataRow has a method that is called IsNull()
that you can use to test the column if it has a null value - regarding to the null as it's seen by the database.
DataRow有一个名为IsNull()的方法,如果列具有null值,您可以使用该方法来测试该列——与数据库中看到的null值有关。
DataRow["col"]==null
will allways be false
.
DataRow["col"]= null将始终为假。
use
使用
DataRow r;
if (r.IsNull("col")) ...
instead.
代替。
#6
3
Null is similar to zero pointer in C++. So it is a reference which not pointing to any value.
在c++中,Null类似于零指针。所以它是一个没有指向任何值的引用。
DBNull.Value
is completely different and is a constant which is returned when a field value contains NULL.
DBNull。值是完全不同的,是当字段值包含NULL时返回的常量。
#1
94
Well, null
is not an instance of any type. Rather, it is an invalid reference.
null不是任何类型的实例。相反,它是一个无效的引用。
However, System.DbNull.Value
, is a valid reference to an instance of System.DbNull
(System.DbNull
is a singleton and System.DbNull.Value
gives you a reference to the single instance of that class) that represents nonexistent* values in the database.
然而,System.DbNull。值,是对系统实例的有效引用。DbNull(系统。DbNull是一个单例和System.DbNull。值为您提供对该类的单个实例的引用),该实例表示数据库中不存在的*值。
*We would normally say null
, but I don't want to confound the issue.
我们通常会说null,但我不想混淆这个问题。
So, there's a big conceptual difference between the two. The keyword null
represents an invalid reference. The class System.DbNull
represents a nonexistent value in a database field. In general, we should try avoid using the same thing (in this case null
) to represent two very different concepts (in this case an invalid reference versus a nonexistent value in a database field).
所以,这两者在概念上有很大的不同。关键字null表示无效引用。类系统。DbNull表示数据库字段中不存在的值。通常,我们应该尽量避免使用相同的东西(在本例中为null)来表示两个非常不同的概念(在本例中是无效引用,而在数据库字段中是不存在的值)。
Keep in mind, this is why a lot of people advocate using the null object pattern in general, which is exactly what System.DbNull
is an example of.
记住,这就是为什么很多人提倡使用空对象模式,这正是系统。DbNull就是一个例子。
#2
20
From the documentation of the DBNull class:
来自DBNull类的文档:
Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.
不要将面向对象编程语言中的null概念与DBNull对象混淆。在面向对象的编程语言中,null意味着没有对对象的引用。DBNull表示未初始化的变量或不存在的数据库列。
#3
9
DBNull.Value is annoying to have to deal with.
DBNull。要处理价值是很烦人的。
I use static methods that check if it's DBNull and then return the value.
我使用静态方法检查它是否为DBNull,然后返回值。
SqlDataReader r = ...;
String firstName = getString(r[COL_Firstname]);
private static String getString(Object o) {
if (o == DBNull.Value) return null;
return (String) o;
}
Also, when inserting values into a DataRow, you can't use "null", you have to use DBNull.Value.
此外,在将值插入DataRow时,不能使用“null”,必须使用DBNull.Value。
Have two representations of "null" is a bad design for no apparent benefit.
有两个“空”的表示是一个糟糕的设计,没有明显的好处。
#4
5
DBNull.Value is what the .NET Database providers return to represent a null entry in the database. DBNull.Value is not null and comparissons to null for column values retrieved from a database row will not work, you should always compare to DBNull.Value.
DBNull。值是. net数据库提供者返回来表示数据库中的空条目的值。DBNull。值不为空,对于从数据库行检索的列值,比较为空将不起作用,您应该始终与DBNull.Value进行比较。
http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx
http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx
#5
3
DataRow has a method that is called IsNull()
that you can use to test the column if it has a null value - regarding to the null as it's seen by the database.
DataRow有一个名为IsNull()的方法,如果列具有null值,您可以使用该方法来测试该列——与数据库中看到的null值有关。
DataRow["col"]==null
will allways be false
.
DataRow["col"]= null将始终为假。
use
使用
DataRow r;
if (r.IsNull("col")) ...
instead.
代替。
#6
3
Null is similar to zero pointer in C++. So it is a reference which not pointing to any value.
在c++中,Null类似于零指针。所以它是一个没有指向任何值的引用。
DBNull.Value
is completely different and is a constant which is returned when a field value contains NULL.
DBNull。值是完全不同的,是当字段值包含NULL时返回的常量。