简体   繁体   English

SqlDataReader 只能检查(Null 或非 null 值)之一

[英]SqlDataReader can only check for one of (Null or non null value)

I am writing a program (for personal use) that stores card names into a database.我正在编写一个程序(供个人使用),将卡名存储到数据库中。 I want to implement a way to see if a name already exists and if it does it will go in a "Do_have_in_db_listbox" else it will go in a "do_not_have_list_box".我想实现一种方法来查看名称是否已经存在,如果存在,它将 go 在“Do_have_in_db_listbox”中,否则它将 go 在“do_not_have_list_box”中。 This is my original method for doing so:这是我这样做的原始方法:

 while (retrieve.Read())
        {
            if(retrieve["Card"] != DBNull.Value)
            {
                listBox_haveInDB.Items.Add(word_for_query);
            }
            else if (retrieve["Card"] == DBNull.Value)
            {
                listBox_notINDb.Items.Add(word_for_query);
            }
        }

I've tried this without the While loop, I've tried variations of if, else, else if and conditions.我在没有 While 循环的情况下尝试过这个,我尝试过 if、else、else if 和条件的变体。 But for whatever reason the else statement NEVER executes no mater what the first condition is.但无论出于何种原因,无论第一个条件是什么,else 语句都不会执行。 I've been looking and trying to trouble shoot but the only thing that worked for me was an exception handler:我一直在寻找并尝试解决问题,但唯一对我有用的是异常处理程序: 截屏

retrieve.Read();
try
          {
              if(retrieve["Card"] != DBNull.Value)
              {
                  listBox_haveInDB.Items.Add(word_for_query);
              }
          }
          catch
          {
              listBox_notINDb.Items.Add(word_for_query);
          }

This is my way of getting around it.这是我解决它的方法。 What am I doing wrong?我究竟做错了什么?

To evaluate database fields to determine whether their values are DBNull, you can pass the field value to the DBNull.Value.Equals method.要评估数据库字段以确定它们的值是否为 DBNull,可以将字段值传递给 DBNull.Value.Equals 方法。 However, this method is rarely used.但是,这种方法很少使用。

 DBNull.Value.Equals(retrieve["Card"] )

Try to use this syntax.尝试使用这种语法。 That's the most reliable way:这是最可靠的方法:

if (!reader.IsDBNull( reader.GetOrdinal("Card")))
{
  listBox_haveInDB.Items.Add( ...your code
}

or you can use index as well或者您也可以使用索引

if (!reader.IsDBNull( column_index )

Is Card a field in the datatable? Card是数据表中的一个字段吗? If there is any data in the datatable, the retrieve["Card"] will never be null.如果数据表中有任何数据, retrieve["Card"]永远不会是 null。

I want to implement a way to see if a name already exists我想实现一种方法来查看名称是否已经存在

If you want to check if word_for_query exists in datatable, you need to compare retrieve["Card"].ToString() with word_for_query , rather than DBNull.Value .如果要检查数据表中是否存在word_for_query ,则需要将retrieve["Card"].ToString()word_for_query进行比较,而不是DBNull.Value

SqlDataReader retrieve = cmd.ExecuteReader();
try
{
    while (retrieve.Read())
    {
        if (retrieve["Card"].ToString() == word_for_query)
        {
            // if exists, show in listBox_haveInDB and return
            listBox_haveInDB.Items.Add(word_for_query);
            return;
        }
    }
    // not exists
    listBox_notINDb.Items.Add(word_for_query);
}
catch (Exception ex)
{
    Console.WriteLine("\nError:\n{0}", ex.Message);
}
finally
{
    retrieve.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM