DBNull Klasa
Definicja
Reprezentuje nieistniejącą wartość.Represents a nonexistent value. Klasa ta nie może być dziedziczona.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
- Dziedziczenie
-
DBNull
- Atrybuty
- Implementuje
Przykłady
Poniższy przykład wywołuje metodę, DBNull.Value.Equals
aby określić, czy pole bazy danych w bazie danych kontaktów ma prawidłową wartość.The following example calls the DBNull.Value.Equals
method to determine whether a database field in a contacts database has a valid value. Jeśli tak, wartość pola jest dołączana do ciągu wyjściowego w etykiecie.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
Uwagi
DBNull Klasa reprezentuje nieistniejącą wartość.The DBNull class represents a nonexistent value. Na przykład w bazie danych kolumna w wierszu tabeli może nie zawierać żadnych danych.In a database, for example, a column in a row of a table might not contain any data whatsoever. Oznacza to, że kolumna jest uważana za nieistniejącą zamiast tylko nie ma wartości.That is, the column is considered to not exist at all instead of merely not having a value. DBNull Obiekt reprezentuje nieistniejącą kolumnę.A DBNull object represents the nonexistent column. Ponadto międzyoperacyjność com używa DBNull klasy do rozróżniania między VT_NULL Variant, która wskazuje nieistniejącą wartość, a VT_EMPTY Variant, która wskazuje nieokreśloną wartość.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.
Typ jest klasą pojedynczą, co oznacza, że DBNull istnieje tylko jeden obiekt. DBNullThe DBNull type is a singleton class, which means only one DBNull object exists. Element członkowski reprezentuje jedyny DBNull obiekt. DBNull.ValueThe DBNull.Value member represents the sole DBNull object. DBNull.Valuemoże służyć do jawnego przypisywania nieistniejącej wartości do pola bazy danych, chociaż większość dostawców danych ADO.NET automatycznie przypisuje DBNull wartości, gdy pole nie ma prawidłowej wartości.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. Można określić, czy wartość pobieraną z pola bazy danych jest DBNull wartością przez przekazanie wartości tego pola DBNull.Value.Equals
do metody.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. Jednak niektóre języki i obiekty bazy danych dostarczają metody, które ułatwiają określenie, czy wartość pola bazy danych jest DBNull.Value.However, some languages and database objects supply methods that make it easier to determine whether the value of a database field is DBNull.Value. Obejmują one funkcję Visual Basic IsDBNull
Convert.IsDBNull , metodę, DataTableReader.IsDBNull metodę i IDataRecord.IsDBNull metodę.These include the Visual Basic IsDBNull
function, the Convert.IsDBNull method, the DataTableReader.IsDBNull method, and the IDataRecord.IsDBNull method.
Nie należy mylić koncepcji null
w języku programowania zorientowanego obiektowo DBNull z obiektem.Do not confuse the notion of null
in an object-oriented programming language with a DBNull object. W języku null
programowania zorientowanego obiektowo oznacza brak odwołania do obiektu.In an object-oriented programming language, null
means the absence of a reference to an object. DBNullreprezentuje niezainicjowaną odmianę lub nieistniejącą kolumnę bazy danych.DBNull represents an uninitialized variant or nonexistent database column.
Pola
Value |
Reprezentuje pojedyncze wystąpienie DBNull klasy.Represents the sole instance of the DBNull class. |
Metody
Equals(Object) |
Określa, czy określony obiekt jest równy bieżącemu obiektowi.Determines whether the specified object is equal to the current object. (Odziedziczone po Object) |
GetHashCode() |
Służy jako domyślna funkcja skrótu.Serves as the default hash function. (Odziedziczone po Object) |
GetObjectData(SerializationInfo, StreamingContext) |
Implementuje interfejs i zwraca dane, które są konieczne do DBNull serializacji obiektu. ISerializableImplements the ISerializable interface and returns the data needed to serialize the DBNull object. |
GetType() |
Pobiera Type bieżącego wystąpienia.Gets the Type of the current instance. (Odziedziczone po Object) |
GetTypeCode() |
Pobiera wartość dla DBNull. TypeCodeGets the TypeCode value for DBNull. |
MemberwiseClone() |
Tworzy skróconą kopię bieżącego Object.Creates a shallow copy of the current Object. (Odziedziczone po Object) |
ToString() | |
ToString(IFormatProvider) |
Zwraca pusty ciąg, używając określonego IFormatProviderelementu.Returns an empty string using the specified IFormatProvider. |
Jawne implementacje interfejsu
IConvertible.ToBoolean(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToByte(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToChar(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToDateTime(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToDecimal(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToDouble(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToInt16(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToInt32(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToInt64(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToSByte(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToSingle(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToType(Type, IFormatProvider) |
Konwertuje bieżący DBNull obiekt na określony typ.Converts the current DBNull object to the specified type. |
IConvertible.ToUInt16(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToUInt32(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |
IConvertible.ToUInt64(IFormatProvider) |
Ta konwersja nie jest obsługiwana.This conversion is not supported. Podjęto próbę wykonania tej konwersji InvalidCastException.Attempting to make this conversion throws an InvalidCastException. |