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

时间:2018-09-04 23:59:25

标签: c# mysql

我知道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();

2 个答案:

答案 0 :(得分:1)

要使用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 = "";
    }
  }
}

答案 1 :(得分:0)

您可以这样做:

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

或者您可以使用类似SO Post的扩展方法: