列中的空值

时间:2013-06-25 12:57:48

标签: c# winforms

我的数据库中有多列表。我想检查某列是否具有空值。我使用了这段代码,但它只适用于第一个,而不是第二个。

if (!read.IsDBNull(3))
{
  lblInc3.Visible = true;
  txtInc3.Visible = true;
  lblInc3.Text = read["Income3"].ToString();
}
else if (!read.IsDBNull(4))
{
  lblInc4.Visible = true;
  txtInc4.Visible = true;
  lblInc4.Text = read["Income4"].ToString();
} 
else 
{
  txtInc3.Visible = false;
  txtInc4.Visible = false;
}

如果多列没有空值,我只想要执行逻辑。如果它具有空值,则文本框不应该是可见的。

修改

      try
        {
            SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
            sqlCon.Open();

            SqlCommand com = new SqlCommand("SELECT * FROM Allowance", sqlCon);

            SqlParameter income1 = new SqlParameter("@Income1", SqlDbType.VarChar, 20);
            com.Parameters.Add(income1);
            income1.Direction = ParameterDirection.Output;

            SqlParameter income2 = new SqlParameter("@Income2", SqlDbType.VarChar, 20);
            com.Parameters.Add(income2);
            income2.Direction = ParameterDirection.Output;

            SqlParameter income3 = new SqlParameter("@Income3", SqlDbType.VarChar, 20);
            com.Parameters.Add(income3);
            income3.Direction = ParameterDirection.Output;

            SqlParameter income4 = new SqlParameter("@Income4", SqlDbType.VarChar, 20);
            com.Parameters.Add(income4);
            income4.Direction = ParameterDirection.Output;

            SqlParameter income5 = new SqlParameter("@Income5", SqlDbType.VarChar, 20);
            com.Parameters.Add(income5);
            income5.Direction = ParameterDirection.Output;

            SqlDataReader read = com.ExecuteReader();

            while (read.Read())
            {
                bool threeBool = !read.IsDBNull(1) ? true : false;

                lblInc1.Visible = threeBool;
                txtInc1.Visible = threeBool;
                lblInc1.Text = threeBool ? read["Income1"].ToString() : string.Empty;


                bool fourBool = !read.IsDBNull(4) ? true : false;

                lblInc4.Visible = fourBool;
                txtInc4.Visible = fourBool;
                lblInc4.Text = fourBool ? read["Income4"].ToString() : string.Empty;

                /*if (!read.IsDBNull(1))
                {
                    lblInc1.Visible = true;
                    txtInc1.Visible = true;
                    lblInc1.Text = read["Income1"].ToString();
                }
                else
                {
                    txtInc1.Visible = false;
                }

                if (!read.IsDBNull(2))
                {
                    lblInc2.Visible = true;
                    txtInc2.Visible = true;
                    lblInc2.Text = read["Income2"].ToString();
                }
                else
                {
                    txtInc2.Visible = false;
                }

                if (!read.IsDBNull(3))
                {
                    lblInc3.Visible = true;
                    txtInc3.Visible = true;
                    lblInc3.Text = read["Income3"].ToString();
                }
                else
                {
                    txtInc3.Visible = false;
                }

                if (!read.IsDBNull(4))
                {
                    lblInc4.Visible = true;
                    txtInc4.Visible = true;
                    lblInc4.Text = read["Income4"].ToString();
                }
                else
                {
                    txtInc4.Visible = false;
                }

                if (!read.IsDBNull(5))
                {
                    lblInc5.Visible = true;
                    txtInc5.Visible = true;
                    lblInc5.Text = read["Income5"].ToString();
                }
                else
                {
                    txtInc5.Visible = false;
                }*/
            } catch (Exception ex) {
               throw;
            }

3 个答案:

答案 0 :(得分:5)

它不适用于第二个,因为您使用的是else-if,并且在第一个比较评估为true后语句被短路。实现第二个if。 e.g。

if (!read.IsDBNull(3))
{
  lblInc3.Visible = true;
  txtInc3.Visible = true;
  lblInc3.Text = read["Income3"].ToString();
} else
{
   txtInc3.Visible = false;
}

if (!read.IsDBNull(4))
{
  lblInc4.Visible = true;
  txtInc4.Visible = true;
  lblInc4.Text = read["Income4"].ToString();
}  else
{
   txtInc4.Visible = false;
}

答案 1 :(得分:1)

也许是这样的,我希望这有帮助(我没有测试这个:))。

编辑:根据您的评论,我相信这就是您想要的。

 bool threeBool = !string.IsNullOrEmpty(read["Income3"].ToString());

  lblInc3.Visible = threeBool ;
  txtInc3.Visible = threeBool ;
  lblInc3.Text = threeBool  ? read["Income3"].ToString() : string.empty;


  bool fourBool = !string.IsNullOrEmpty(read["Income4"].ToString());

  lblInc4.Visible = fourBool ;
  txtInc4.Visible = fourBool ;
  lblInc4.Text = fourBool ? read["Income4"].ToString() : string.empty;

答案 2 :(得分:0)

删除Else部分,如果第一部分为True,则跳过其他部分......

if (!read.IsDBNull(3))
{
  lblInc3.Visible = true;
  txtInc3.Visible = true;
  lblInc3.Text = read["Income3"].ToString();
}

if (!read.IsDBNull(4))
{
  lblInc4.Visible = true;
  txtInc4.Visible = true;
  lblInc4.Text = read["Income4"].ToString();
}