为DBNull的要点是什么? [英] What is the point of DBNull?

查看:175
本文介绍了为DBNull的要点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中存在的的参考,这是用来无处不表示对象引用是空的,再有就是的DBNull ,这是由数据库驱动程序(和其他一些)表示... pretty的同样的事情。当然,这创造了很多的困惑和转换程序已被搅动,等。

In .NET there is the null reference, which is used everywhere to denote that an object reference is empty, and then there is the DBNull, which is used by database drivers (and few others) to denote... pretty much the same thing. Naturally, this creates a lot of confusion and conversion routines have to be churned out, etc.

那么,为什么原来的.NET笔者决定做这个?对我来说没有任何意义。他们的文件是没有意义之一:

So why did the original .NET authors decide to make this? To me it makes no sense. Their documentation makes no sense either:

DBNull类重presents一个不存在的价值。在数据库中,例如,在一排一个表的一列可能不会任何包含任何数据。也就是说,列被认为是不存在的,而不是仅仅不具有值的所有。再$ P $ A的DBNull对象psents不存在的列。此外,COM互操作使用DBNull类一个VT_NULL变体,这表明不存在的值,和一个VT_EMPTY变体,这表明未指定的值来区分。

The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column. Additionally, COM interop uses the DBNull class to distinguish between a VT_NULL variant, which indicates a nonexistent value, and a VT_EMPTY variant, which indicates an unspecified value.

这是什么废话关于列不存在? A柱的存在,它只是不具有特定行的值。如果它不存在,我会得到一个异常试图访问特定的细胞,而不是一个的DBNull !我可以理解,需要区分 VT_NULL VT_EMPTY ,但为什么不把一个 COMEmpty 类呢?这将是在整个.NET框架的一个非常巧妙的配合。

What's this crap about a "column not existing"? A column exists, it just doesn't have a value for the particular row. If it didn't exist, I'd get an exception trying to access the specific cell, not a DBNull! I can understand the need to differentiate between VT_NULL and VT_EMPTY, but then why not make a COMEmpty class instead? That would be a much neater fit in the whole .NET framework.

我缺少的东西?任何人都可以提供一些线索,为什么的DBNull 的发明,它可以帮助解决什么问题?

Am I missing something? Can anyone shed some light why DBNull was invented and what problems it helps to solve?

推荐答案

的一点是,在某些情况下存在一个数据库值之间的差是零和.NET空。

The point is that in certain situations there is a difference between a database value being null and a .NET Null.

例如。如果您使用的ExecuteScalar(返回第一行的第一列的结果集),你会得到一个空回这意味着执行的SQL没有返回任何值。如果你得到的DBNull回来就意味着价值是由SQL返回的,它是NULL。你必须能够分辨出来。

For example. If you using ExecuteScalar (which returns the first column of the first row in the result set) and you get a null back that means that the SQL executed did not return any values. If you get DBNull back it means a value was returned by the SQL and it was NULL. You need to be able to tell the difference.

这篇关于为DBNull的要点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆