DBNull DBNull DBNull DBNull Class

定义

表示不存在的值。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
继承
DBNullDBNullDBNullDBNull
属性
实现

示例

下面的示例调用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 Value Value Value

表示 DBNull 类的唯一实例。Represents the sole instance of the DBNull class.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

实现 ISerializable 接口并返回序列化 DBNull 对象所需的数据。Implements the ISerializable interface and returns the data needed to serialize the DBNull object.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
GetTypeCode() GetTypeCode() GetTypeCode() GetTypeCode()

获取 TypeCodeDBNull 值。Gets the TypeCode value for DBNull.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回空字符串(Empty)。Returns an empty string (Empty).

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider)

使用指定的 IFormatProvider 返回空字符串。Returns an empty string using the specified IFormatProvider.

显式界面实现

IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider)

将当前的 DBNull 对象转换为指定类型。Converts the current DBNull object to the specified type.

IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider)

不支持此转换。This conversion is not supported. 如果尝试执行此转换,便会引发 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

适用于

另请参阅