Redigeeri

Jagamisviis:


DBNull Class

Definition

Represents a nonexistent value. This class cannot be inherited.

public ref class DBNull sealed
public ref class DBNull sealed : IConvertible, System::Runtime::Serialization::ISerializable
public sealed class DBNull
public sealed class DBNull : IConvertible, System.Runtime.Serialization.ISerializable
[System.Serializable]
public sealed class DBNull : IConvertible, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DBNull : IConvertible, System.Runtime.Serialization.ISerializable
type DBNull = class
type DBNull = class
    interface IConvertible
    interface ISerializable
[<System.Serializable>]
type DBNull = class
    interface ISerializable
    interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DBNull = class
    interface ISerializable
    interface IConvertible
Public NotInheritable Class DBNull
Public NotInheritable Class DBNull
Implements IConvertible, ISerializable
Inheritance
DBNull
Attributes
Implements

Examples

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;
}
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

Remarks

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. A DBNull object represents the nonexistent column. 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.

The DBNull type is a singleton class, which means only one DBNull object exists. The DBNull.Value member represents the sole DBNull object. 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. 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. However, some languages and database objects supply methods that make it easier to determine whether the value of a database field is DBNull.Value. These include the Visual Basic IsDBNull function, the Convert.IsDBNull method, the DataTableReader.IsDBNull method, and the IDataRecord.IsDBNull method.

Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.

Fields

Value

Represents the sole instance of the DBNull class.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

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

Implements the ISerializable interface and returns the data needed to serialize the DBNull object.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetTypeCode()

Gets the TypeCode value for DBNull.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns an empty string (Empty).

ToString(IFormatProvider)

Returns an empty string using the specified IFormatProvider.

Explicit Interface Implementations

IConvertible.ToBoolean(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToByte(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToChar(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToDateTime(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToDecimal(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToDouble(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt16(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt32(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToInt64(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToSByte(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToSingle(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToType(Type, IFormatProvider)

Converts the current DBNull object to the specified type.

IConvertible.ToUInt16(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToUInt32(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

IConvertible.ToUInt64(IFormatProvider)

This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.

Applies to

See also