我应该使用isdbnull吗?在这种特殊情况下如何使用它?

j2cgzkjk  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(430)

我知道 Null 值不能被转换成字符串值,所以我在网络上到处寻找,并找到了一些答案,比如使用 IsDBNull() 但我好像没法让它工作。我是新来的,所以请容忍我。我只需要在适当的 TextBox 作为 "" 当它是 Null . 如果你能帮我掌握正确的语法,那就太好了。可能是 Null (或否)是 projects_project_number ,我返回到我的原始代码,以显示我的开始。在这里:

ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
MySqlConnection con = new MySqlConnection(conSettings.ToString());
string Query = "select * from shopmanager.quotes where idquotes = @search_quote_number;";

MySqlCommand cmdDataBase = new MySqlCommand(Query, con);
MySqlDataReader myReader;
try
{
    con.Open();

    cmdDataBase.Parameters.AddWithValue("@search_quote_number", search_quote_number.Text);
    myReader = cmdDataBase.ExecuteReader();

    while (myReader.Read())
    {
        string sClient_number = myReader.GetString("client_info_client_number");
        string sId = myReader.GetInt16("idquotes").ToString();
        string sClientName = myReader.GetString("client_name");
        string spredicted_start_date = myReader.GetString("predicted_start_date");
        string sdate_required = myReader.GetString("requested_date");
        string sdate_predicted = myReader.GetString("delivery_expected_date");
        string sdate_received = myReader.GetString("date_received");
        string squote_amount = myReader.GetString("quote_amount");
        string sproject_number = myReader.GetString("projects_project_number");

        quote_id.Text = sId;
        client_name.Text = sClientName;
        predicted_start_date.Text = spredicted_start_date;
        date_required.Text = sdate_required;
        date_predicted.Text = sdate_predicted;
        date_received.Text = sdate_received;
        quote_amount.Text = squote_amount;
        project_number.Text = sproject_number;

        temp_client_id.client_id = sClient_number;
        search_quote_number.Text = "";
    }
    cmdDataBase.Parameters.Clear();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
con.Close();
cclgggtu

cclgggtu1#

您可以这样做:

string sproject_number = DBNull.Value.Equals(myReader.GetString("projects_project_number")) ? "" : myReader.GetString("projects_project_number");

或者你也可以使用这样的扩展方法:

a1o7rhls

a1o7rhls2#

要使用dbnull,请遵循msdn上关于is.dbnull()的指南
有多种方法可以使用 DBNull 但利用你在问题中给出的信息,最好的方法是:

string sproject_number = string.Empty;

if (!Convert.IsDBNull(myReader.GetString("projects_project_number"))) //check if value is not null
        sproject_number = myReader.GetString("projects_project_number"); 

project_number.Text = sproject_number;

也可以使用
SqlDataReader .IsDBNull :

int columnIndex = myReader.GetOrdinal("projects_project_number"); //get column index
string sproject_number = string.Empty;

if (!myReader.IsDBNull(columnIndex)) //check if value is not null
        sproject_number = myReader[columnIndex].ToString(); 

project_number.Text = sproject_number;

为完整起见:

int columnIndex = myReader.GetOrdinal();
string sproject_number = string.Empty;

if (DBNull.Value.Equals(myReader.GetString("projects_project_number"))) //check if value is not null
        sproject_number = myReader.GetString("projects_project_number");

project_number.Text = sproject_number;

在一篇关于直接分配到文本框并整理所有内容(并将其添加到 using 阻止是一种好的做法):

ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"]
string Query = "select * from shopmanager.quotes where idquotes = @search_quote_number;";
using(MySqlConnection con = new MySqlConnection(conSettings.ToString()))
{
  using(MySqlCommand cmdDataBase = new MySqlCommand(Query, con))
  {
    cmdDataBase.Parameters.AddWithValue("@search_quote_number", search_quote_number.Text.Trim());
    con.Open();
    var myReader = cmdDataBase.ExecuteReader();

    if(myReader.Read())
    {
      temp_client_id.client_id = myReader.GetString("client_info_client_number");
      quote_id.Text = myReader.GetInt16("idquotes").ToString();
      client_name.Text = myReader.GetString("client_name");
      predicted_start_date.Text = myReader.GetString("predicted_start_date");
      date_required.Text = sdate_required = myReader.GetString("requested_date");
      date_predicted.Tex = myReader.GetString("delivery_expected_date");
      date_received.Text = myReader.GetString("date_received");
      quote_amount.Text = myReader.GetString("quote_amount");
      project_number.Text = DBNull.Value.Equals(myReader.GetString("projects_project_number")) ? "" : myReader.GetString("projects_project_number");

      search_quote_number.Text = "";
    }
  }
}

相关问题