if (txt_number.Text.Trim() != "")
                {
                    if (sqlwhere == "")
                    {
                        sqlwhere = " where PONumber like '%" + txt_number.Text.Trim() + "%'";
                    }
                    else
                    {
                        sqlwhere += " and PONumber like '%" + txt_number.Text.Trim() + "%'";
                    }
                }                if (txt_chestno.Text.Trim() != "")
                {
                    if (sqlwhere == "")
                    {
                        sqlwhere = "where PackNo like '%" + txt_chestno.Text.Trim() + "%'";
                    }
                    else
                    {
                        sqlwhere += " and PackNo like '%" + txt_chestno.Text.Trim() + "%'";
                    }
                }
                if (cob_model.Text.Trim() != "")
                {
                    if (sqlwhere == "")
                    {
                        sqlwhere = "where ProductModel like '%" + cob_model.Text.Trim() + "%'";
                    }
                    else
                    {
                        sqlwhere += " and ProductModel like '%" + cob_model.Text.Trim() + "%'";
                    }
                }
                if (sqlwhere == "")
                {
                    sqlwhere = "where ProjectType ='" + cob_select.Text.Trim() + "'";
                }
                else
                {
                    sqlwhere += " and ProjectType ='" + cob_select.Text.Trim() + "'";
                }这段代码可以写成别的形式吗?不用if语句,用别的方法能实现现在的目的吗?

解决方案 »

  1.   

    因为要对每一个text进行判断拼接,也只能这样了
    或者你搞个foreach遍历所有组件,判断是不是你需要的,然后拼接,可以在textbox的tag属性中保存一些信息,根据此信息得到你要的内容进行拼接
      

  2.   


    //这样是不是清爽多了?
    sqlwhere  += " where 1=1 "if (txt_number.Text.Trim() != "")
      {
      sqlwhere += " and PONumber like '%" + txt_number.Text.Trim() + "%'";
      }  if (txt_chestno.Text.Trim() != "")
      {
       sqlwhere += " and PackNo like '%" + txt_chestno.Text.Trim() + "%'";
      }
      if (cob_model.Text.Trim() != "")
      {
      sqlwhere += " and ProductModel like '%" + cob_model.Text.Trim() + "%'";
      }  sqlwhere += " and ProjectType ='" + cob_select.Text.Trim() + "'";