共用方式為


DBNull.Value 欄位

定義

表示 DBNull 類別的唯一執行個體。

public: static initonly DBNull ^ Value;
public static readonly DBNull Value;
 staticval mutable Value : DBNull
Public Shared ReadOnly Value As DBNull 

欄位值

範例

下列範例會 DBNull.Value.Equals 呼叫 方法來判斷連絡人資料庫中的資料庫欄位是否具有有效的值。 如果這樣做,則會將域值附加至標籤中的字串輸出。

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;
}
member this.OutputLabels(dt: DataTable) =
    let mutable label = ""

    // Iterate rows of table
    for row in dt.Rows do
        let mutable label = String.Empty
        label <- label + this.AddFieldValue(label, row, "Title")
        label <- label + this.AddFieldValue(label, row, "FirstName")
        label <- label + this.AddFieldValue(label, row, "MiddleInitial")
        label <- label + this.AddFieldValue(label, row, "LastName")
        label <- label + this.AddFieldValue(label, row, "Suffix")
        label <- label + "\n"
        label <- label + this.AddFieldValue(label, row, "Address1")
        label <- label + this.AddFieldValue(label, row, "AptNo")
        label <- label + "\n"
        let labelLen = label.Length
        label <- label + this.AddFieldValue(label, row, "Address2")
        let labelLen =
            if label.Length <> labelLen then
                label + "\n"
            else label
        label <- label + this.AddFieldValue(label, row, "City")
        label <- label + this.AddFieldValue(label, row, "State")
        label <- label + this.AddFieldValue(label, row, "Zip")
        printfn $"{label}"
        printfn ""

member _.AddFieldValue(label: string, row: DataRow, fieldName: string) =
    if DBNull.Value.Equals row[fieldName] |> not then
        (string row[fieldName]) + " "
    else
        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 是單一類別,這表示只有這個類別的實例可以存在。

如果資料庫欄位有遺漏的資料,您可以使用 DBNull.Value 屬性明確地將物件值指派 DBNull 給欄位。 不過,大部分的資料提供者都會自動執行這項操作。

若要評估資料庫欄位,以判斷其值是否為 DBNull ,您可以將域值傳遞至 DBNull.Value.Equals 方法。 不過,此方法很少使用,因為有一些其他方法來評估資料庫欄位是否有遺漏資料。 其中包括 Visual Basic IsDBNull 函式、 Convert.IsDBNull 方法、 DataTableReader.IsDBNull 方法、 IDataRecord.IsDBNull 方法,以及數個其他方法。

適用於