Bagikan melalui


DBNull.Value Bidang

Definisi

Mewakili satu-satunya DBNull instans kelas.

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

Nilai Bidang

Contoh

Contoh berikut memanggil DBNull.Value.Equals metode untuk menentukan apakah bidang database dalam database kontak memiliki nilai yang valid. Jika ya, nilai bidang ditambahkan ke output string dalam 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;
}
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

Keterangan

DBNull adalah kelas singleton, yang berarti hanya instans kelas ini yang dapat ada.

Jika bidang database memiliki data yang hilang, Anda bisa menggunakan DBNull.Value properti untuk secara eksplisit menetapkan DBNull nilai objek ke bidang . Namun, sebagian besar penyedia data melakukan ini secara otomatis.

Untuk mengevaluasi bidang database untuk menentukan apakah nilainya , DBNullAnda dapat meneruskan nilai bidang ke DBNull.Value.Equals metode . Namun, metode ini jarang digunakan karena ada sejumlah cara lain untuk mengevaluasi bidang database untuk data yang hilang. Ini termasuk fungsi Visual Basic IsDBNull , Convert.IsDBNull metode , DataTableReader.IsDBNull metode , IDataRecord.IsDBNull metode , dan beberapa metode lainnya.

Berlaku untuk