DBNull.Value Feld

Definition

Stellt die einzige Instanz der DBNull-Klasse dar.Represents the sole instance of the DBNull class.

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

Feldwert

Beispiele

Im folgenden Beispiel wird die DBNull.Value.Equals -Methode aufgerufen, um zu bestimmen, ob ein Datenbankfeld in einer Contacts-Datenbank über einen gültigen Wert verfügt.The following example calls the DBNull.Value.Equals method to determine whether a database field in a contacts database has a valid value. Wenn dies der Fall ist, wird der Feldwert an die Zeichen folgen Ausgabe in einer Bezeichnung angehängt.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

Hinweise

DBNullist eine Singleton-Klasse. Dies bedeutet, dass nur diese Instanz dieser Klasse vorhanden sein kann.DBNull is a singleton class, which means only this instance of this class can exist.

Wenn in einem Datenbankfeld fehlende Daten vorhanden sind, können DBNull.Value Sie die-Eigenschaft verwenden DBNull , um dem Feld explizit einen-Objektwert zuzuweisen.If a database field has missing data, you can use the DBNull.Value property to explicitly assign a DBNull object value to the field. Die meisten Datenanbieter führen dies jedoch automatisch durch.However, most data providers do this automatically.

Wenn Sie Datenbankfelder auswerten möchten, um zu DBNullbestimmen, ob ihre Werte sind, können Sie DBNull.Value.Equals den Feldwert an die-Methode übergeben.To evaluate database fields to determine whether their values are DBNull, you can pass the field value to the DBNull.Value.Equals method. Diese Methode wird jedoch nur selten verwendet, da es eine Reihe weiterer Möglichkeiten gibt, ein Datenbankfeld für fehlende Daten auszuwerten.However, this method is rarely used because there are a number of other ways to evaluate a database field for missing data. Hierzu gehören die Visual Basic IsDBNull -Funktion, Convert.IsDBNull die-Methode DataTableReader.IsDBNull , die- IDataRecord.IsDBNull Methode, die-Methode und verschiedene andere Methoden.These include the Visual Basic IsDBNull function, the Convert.IsDBNull method, the DataTableReader.IsDBNull method, the IDataRecord.IsDBNull method, and several other methods.

Gilt für: