DBNull 类
定义
表示不存在的值。Represents a nonexistent value. 此类不能被继承。This class cannot be inherited.
public ref class DBNull sealed : IConvertible, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class DBNull : IConvertible, System.Runtime.Serialization.ISerializable
type DBNull = class
interface ISerializable
interface IConvertible
Public NotInheritable Class DBNull
Implements IConvertible, ISerializable
- 继承
-
DBNull
- 属性
- 实现
示例
下面的示例调用DBNull.Value.Equals
方法来确定联系人数据库中的数据库字段是否具有有效的值。The following example calls the DBNull.Value.Equals
method to determine whether a database field in a contacts database has a valid value. 如果是这样,则将字段值追加到标签中的字符串输出。If it does, the field value is appended to the string output in a label.
private void OutputLabels(DataTable dt)
{
string label;
// Iterate rows of table
foreach (DataRow row in dt.Rows)
{
int labelLen;
label = String.Empty;
label += AddFieldValue(label, row, "Title");
label += AddFieldValue(label, row, "FirstName");
label += AddFieldValue(label, row, "MiddleInitial");
label += AddFieldValue(label, row, "LastName");
label += AddFieldValue(label, row, "Suffix");
label += "\n";
label += AddFieldValue(label, row, "Address1");
label += AddFieldValue(label, row, "AptNo");
label += "\n";
labelLen = label.Length;
label += AddFieldValue(label, row, "Address2");
if (label.Length != labelLen)
label += "\n";
label += AddFieldValue(label, row, "City");
label += AddFieldValue(label, row, "State");
label += AddFieldValue(label, row, "Zip");
Console.WriteLine(label);
Console.WriteLine();
}
}
private string AddFieldValue(string label, DataRow row,
string fieldName)
{
if (! DBNull.Value.Equals(row[fieldName]))
return (string) row[fieldName] + " ";
else
return String.Empty;
}
Private Sub OUtputLabels(dt As DataTable)
Dim label As String
' Iterate rows of table
For Each row As DataRow In dt.Rows
Dim labelLen As Integer
label = String.Empty
label += AddFieldValue(label, row, "Title")
label += AddFieldValue(label, row, "FirstName")
label += AddFieldValue(label, row, "MiddleInitial")
label += AddFieldValue(label, row, "LastName")
label += AddFieldValue(label, row, "Suffix")
label += vbCrLf
label += AddFieldValue(label, row, "Address1")
label += AddFieldValue(label, row, "AptNo")
label += vbCrLf
labelLen = Len(label)
label += AddFieldValue(label, row, "Address2")
If Len(label) <> labelLen Then label += vbCrLf
label += AddFieldValue(label, row, "City")
label += AddFieldValue(label, row, "State")
label += AddFieldValue(label, row, "Zip")
Console.WriteLine(label)
Console.WriteLine()
Next
End Sub
Private Function AddFieldValue(label As String, row As DataRow, _
fieldName As String) As String
If Not DbNull.Value.Equals(row.Item(fieldName)) Then
Return CStr(row.Item(fieldName)) & " "
Else
Return Nothing
End If
End Function
注解
DBNull类表示不存在的值。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. DBNull对象表示不存在的列。A DBNull object represents the nonexistent column. 此外,COM 互操作使用DBNull类来区分 VT_NULL 变量(指示不存在的值)和 VT_EMPTY 变量(指示未指定的值)。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.
类型为单一实例类,这意味着只存在一个DBNull对象。 DBNullThe DBNull type is a singleton class, which means only one DBNull object exists. 成员表示唯一DBNull的对象。 DBNull.ValueThe DBNull.Value member represents the sole DBNull object. DBNull.Value可用于显式将不存在的值分配给数据库字段,不过,大多数 ADO.NET 数据访问接口DBNull在字段没有有效值时自动分配值。DBNull.Value can be used to explicitly assign a nonexistent value to a database field, although most ADO.NET data providers automatically assign values of DBNull when a field does not have a valid value. 您可以通过将字段的值传递DBNull DBNull.Value.Equals
给方法来确定从数据库字段中检索到的值是否是一个值。You can determine whether a value retrieved from a database field is a DBNull value by passing the value of that field to the DBNull.Value.Equals
method. 但是,某些语言和数据库对象提供一些方法,使您可以更轻松地确定数据库字段的值是否DBNull.Value为。However, some languages and database objects supply methods that make it easier to determine whether the value of a database field is DBNull.Value. 其中包括 Visual Basic IsDBNull
函数Convert.IsDBNull 、方法、 DataTableReader.IsDBNull方法和IDataRecord.IsDBNull方法。These include the Visual Basic IsDBNull
function, the Convert.IsDBNull method, the DataTableReader.IsDBNull method, and the IDataRecord.IsDBNull method.
不要将面向对象的编程null
语言中的概念DBNull与对象混淆。Do not confuse the notion of null
in an object-oriented programming language with a DBNull object. 在面向对象的编程语言中, null
表示缺少对对象的引用。In an object-oriented programming language, null
means the absence of a reference to an object. DBNull表示未初始化的变量或不存在的数据库列。DBNull represents an uninitialized variant or nonexistent database column.
字段
Value |
表示 DBNull 类的唯一实例。Represents the sole instance of the DBNull class. |
方法
Equals(Object) |
确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
GetHashCode() |
用作默认哈希函数。Serves as the default hash function. (继承自 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
实现 ISerializable 接口并返回序列化 DBNull 对象所需的数据。Implements the ISerializable interface and returns the data needed to serialize the DBNull object. |
GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
GetTypeCode() | |
MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
ToString() | |
ToString(IFormatProvider) |
使用指定的 IFormatProvider 返回空字符串。Returns an empty string using the specified IFormatProvider. |
显式界面实现
IConvertible.ToBoolean(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToByte(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToChar(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToDateTime(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToDecimal(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToDouble(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToInt16(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToInt32(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToInt64(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToSByte(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToSingle(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToType(Type, IFormatProvider) |
将当前的 DBNull 对象转换为指定类型。Converts the current DBNull object to the specified type. |
IConvertible.ToUInt16(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToUInt32(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToUInt64(IFormatProvider) |
不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastException。Attempting to make this conversion throws an InvalidCastException. |