Convert.IsDBNull(Object) Convert.IsDBNull(Object) Convert.IsDBNull(Object) Convert.IsDBNull(Object) Method


指定したオブジェクトの型が DBNull かどうかを示す値を返します。Returns an indication whether the specified object is of type DBNull.

 static bool IsDBNull(System::Object ^ value);
public static bool IsDBNull (object value);
static member IsDBNull : obj -> bool
Public Shared Function IsDBNull (value As Object) As Boolean


Object Object Object Object

オブジェクト。An object.


true の型が value の場合は DBNull。それ以外の場合は falsetrue if value is of type DBNull; otherwise, false.

次の例では、SqlDataReader調査データをデータベースから取得するオブジェクト。The following example uses a SqlDataReader object to retrieve survey data from a database. 配列では、各行のフィールドの値が割り当てられ、各パスの要素を配列し、IsDBNullメソッド。It assigns each row's field values to an array, and then passes each array element to the IsDBNull method. メソッドを返す場合true例では、配列要素に文字列"NA"を割り当てています。If the method returns true, the example assigns the string "NA" to the array element. 配列に追加し、RowsのコレクションをSystem.Windows.Forms.DataGridViewコントロール。The array is then added to the Rows collection of a System.Windows.Forms.DataGridView control.

private void Form1_Load(object sender, EventArgs e)
   // Define ADO.NET objects.
   SqlConnection conn = new SqlConnection(connectionString);
   SqlCommand cmd = new SqlCommand();
   SqlDataReader dr;

   // Open connection, and retrieve dataset.

   // Define Command object.
   cmd.CommandText = "Select * From Responses";
   cmd.CommandType = CommandType.Text;
   cmd.Connection = conn;

   // Retrieve data reader.
   dr = cmd.ExecuteReader();

   int fieldCount = dr.FieldCount;
   object[] fieldValues = new object[fieldCount];
   string[] headers = new string[fieldCount];
   // Get names of fields.
   for (int ctr = 0; ctr < fieldCount; ctr++)
      headers[ctr] = dr.GetName(ctr);

   // Set up data grid.
   this.grid.ColumnCount = fieldCount;

   this.grid.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
   this.grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
   this.grid.ColumnHeadersDefaultCellStyle.Font = new Font(this.grid.Font, FontStyle.Bold);

   this.grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
   this.grid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
   this.grid.CellBorderStyle = DataGridViewCellBorderStyle.Single;
   this.grid.GridColor = Color.Black;
   this.grid.RowHeadersVisible = true;

   for (int columnNumber = 0; columnNumber < headers.Length;  columnNumber++)
      this.grid.Columns[columnNumber].Name = headers[columnNumber];

   // Get data, replace missing values with "NA", and display it.
   while (dr.Read())

      for (int fieldCounter = 0; fieldCounter < fieldCount; fieldCounter++)
         if (Convert.IsDBNull(fieldValues[fieldCounter]))
            fieldValues[fieldCounter] = "NA";
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   ' Define ADO.NET objects.
   Dim conn As New SqlConnection(connectionString)
   Dim cmd As New SqlCommand
   Dim dr As SqlDataReader

   ' Open connection, and retrieve dataset.

   ' Define Command object.
   cmd.CommandText = "Select * From Responses"
   cmd.CommandType = CommandType.Text
   cmd.Connection = conn

   ' Retrieve data reader.
   dr = cmd.ExecuteReader()

   Dim fieldCount As Integer = dr.FieldCount
   Dim fieldValues(fieldCount - 1) As Object
   Dim headers(fieldCount - 1) As String

   ' Get names of fields.
   For ctr As Integer = 0 To fieldCount - 1
      headers(ctr) = dr.GetName(ctr)

   ' Set up data grid.
   grid.ColumnCount = fieldCount

   With grid.ColumnHeadersDefaultCellStyle
      .BackColor = Color.Navy
      .ForeColor = Color.White
      .Font = New Font(grid.Font, FontStyle.Bold)
   End With

   With grid
      .AutoSizeRowsMode = _
      .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
      .CellBorderStyle = DataGridViewCellBorderStyle.Single
      .GridColor = Color.Black
      .RowHeadersVisible = True

      For columnNumber As Integer = 0 To headers.Length - 1
         .Columns(columnNumber).Name = headers(columnNumber)
   End With

   ' Get data, replace missing values with "NA", and display it.
   Do While dr.Read()

      For fieldCounter As Integer = 0 To fieldCount - 1
         If Convert.IsDBNull(fieldValues(fieldCounter)) Then
            fieldValues(fieldCounter) = "NA"
         End If
End Sub


IsDBNullメソッドを調べますかどうか、valueパラメーターが等しくDBNull.Valueします。The IsDBNull method tests whether the value parameter is equal to DBNull.Value. これは、次のコードに相当します。It is equivalent to the following code:

return DBNull.Value.Equals(value);
Return DBNull.Value.Equals(value)


DBNull.Value 不足している値を示すために使用されます。DBNull.Value is used to indicate a value that is missing. 等しくないnullまたはString.Emptyします。It is not equivalent to null or to String.Empty. そのため、コードなどConvert.IsDBNull(null)(C#) またはConvert.IsDBNull(Nothing)Visual Basic 返しますfalseします。Therefore, code such as Convert.IsDBNull(null) in C# or Convert.IsDBNull(Nothing) in Visual Basic returnsfalse.