求高手指点,if的替换

slowly0321 2012-05-07 09:25:23
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语句,用别的方法能实现现在的目的吗?
...全文
140 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2012-05-07
  • 打赏
  • 举报
回复

//这样是不是清爽多了?
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() + "'";

bdmh 2012-05-07
  • 打赏
  • 举报
回复
因为要对每一个text进行判断拼接,也只能这样了
或者你搞个foreach遍历所有组件,判断是不是你需要的,然后拼接,可以在textbox的tag属性中保存一些信息,根据此信息得到你要的内容进行拼接
tigercao101 2012-05-07
  • 打赏
  • 举报
回复
switch()
{case}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧