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.

public:
 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

매개 변수

value
Object Object Object Object

개체입니다.An object.

반환

true의 형식이 value이면 DBNull이고, 그렇지 않으면 false입니다.true 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.
   conn.Open();

   // 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())
   {
      dr.GetValues(fieldValues);

      for (int fieldCounter = 0; fieldCounter < fieldCount; fieldCounter++)
      {
         if (Convert.IsDBNull(fieldValues[fieldCounter]))
            fieldValues[fieldCounter] = "NA";
      }
      grid.Rows.Add(fieldValues);
   }
   dr.Close();
}
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.
   conn.Open()

   ' 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)
   Next

   ' 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 = _
          DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
      .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)
      Next
   End With

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

      For fieldCounter As Integer = 0 To fieldCount - 1
         If Convert.IsDBNull(fieldValues(fieldCounter)) Then
            fieldValues(fieldCounter) = "NA"
         End If
      Next
      grid.Rows.Add(fieldValues)
   Loop
   dr.Close()
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.

적용 대상

추가 정보