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 Interop 會使用 DBNull 類別來區別 VT_Null 變異數,這表示不存在的值,而 VT_EMPTY variant 則表示未指定的值。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 類型是單一類別,這表示只有一個 DBNull 物件存在。The DBNull type is a singleton class, which means only one DBNull object exists. DBNull.Value 成員代表唯一的 DBNull 物件。The 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.Value.Equals 方法,判斷從資料庫欄位抓取的值是否為 DBNull 值。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.ValueHowever, 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 代表未初始化的 variant 或不存在的資料庫資料行。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()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
GetTypeCode()

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

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

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

ToString(IFormatProvider)

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

明確介面實作

IConvertible.ToBoolean(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToByte(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToChar(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToDateTime(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToDecimal(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToDouble(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt16(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt32(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt64(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToSByte(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToSingle(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting 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. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToUInt32(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

IConvertible.ToUInt64(IFormatProvider)

不支援這個轉換。This conversion is not supported. 嘗試進行這個轉換會擲回 InvalidCastExceptionAttempting to make this conversion throws an InvalidCastException.

適用於

另請參閱