Aracılığıyla paylaş


Convert.IsDBNull(Object) Yöntem

Tanım

Belirtilen nesnenin türünde DBNullolup olmadığını gösteren bir gösterge döndürür.

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

Parametreler

value
Object

Bir nesne.

Döndürülenler

true türündeyse valueDBNull; değilse, false.

Örnekler

Aşağıdaki örnek, veritabanından SqlDataReader anket verilerini almak için bir nesnesi kullanır. Her satırın alan değerlerini bir diziye atar ve ardından her dizi öğesini yöntemine IsDBNull geçirir. yöntemi döndürürse true, örnek dizi öğesine "NA" dizesini atar. Dizi daha sonra bir System.Windows.Forms.DataGridView denetim koleksiyonuna Rows eklenir.

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();
}
member this.Form1_Load(sender: obj, e: EventArgs) =
    // Define ADO.NET objects.
    use conn = new SqlConnection(connectionString)
    use cmd = new SqlCommand()

    // 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.
    let dr = cmd.ExecuteReader()

    let fieldCount = dr.FieldCount
    let fieldValues = Array.zeroCreate<obj> fieldCount
    let headers = 
        // Get names of fields.
        Array.init fieldCount dr.GetName

    // Set up data grid.
    grid.ColumnCount <- fieldCount

    grid.ColumnHeadersDefaultCellStyle.BackColor <- Color.Navy
    grid.ColumnHeadersDefaultCellStyle.ForeColor <- Color.White
    grid.ColumnHeadersDefaultCellStyle.Font <- new Font(grid.Font, FontStyle.Bold)

    grid.AutoSizeRowsMode <- DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
    grid.ColumnHeadersBorderStyle <- DataGridViewHeaderBorderStyle.Single
    grid.CellBorderStyle <- DataGridViewCellBorderStyle.Single
    grid.GridColor <- Color.Black
    grid.RowHeadersVisible <- true

    for columnNumber = 0 to headers.Length - 1 do
        grid.Columns[columnNumber].Name <- headers[columnNumber]

    // Get data, replace missing values with "NA", and display it.
    while dr.Read() do
        dr.GetValues fieldValues |> ignore
        for fieldCounter = 0 to fieldCount do
            if Convert.IsDBNull fieldValues[fieldCounter] then
                fieldValues[fieldCounter] <- "NA"
        grid.Rows.Add fieldValues |> ignore
    dr.Close()

override _.Dispose(disposing) =
    if disposing then
        grid.Dispose();
    base.Dispose disposing
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

Açıklamalar

yöntemi, IsDBNull parametresinin value değerine eşit olup olmadığını sınar DBNull.Value. Aşağıdaki koda eşdeğerdir:

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

Not

DBNull.Value eksik bir değeri belirtmek için kullanılır. veya String.Emptyile null eşdeğer değildir. Bu nedenle, C# veya Convert.IsDBNull(Nothing) Visual Basic gibi Convert.IsDBNull(null) kodlar döndürürfalse.

Şunlara uygulanır

Ayrıca bkz.