动态增加sql的select条件

topsecret2000 2008-10-25 09:50:41
有两个combobox,一个存stdid,一个存stdscore,两个都有一个all的值,现在我想有一条select语句,如果combobox1.text=="all"呢,就不把这个combobox加入到查询条件里去,否则呢就加一个 where stdid=combobox1.text
该怎么写呢
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
goldxinx 2008-10-25
  • 打赏
  • 举报
回复
select * from yourtable where stdscore=(case stdscore when @score='' then stdscore else @score) and stuid=(case stdid when @id='' then stdid else @id)
zknyth 2008-10-25
  • 打赏
  • 举报
回复
完全同意二楼观点,同时我觉得可以再简单点:
string sql="select * from yourtable where 1=1";
if(combobox1.Text=="all") sql += " and stdid="+bombobox1.Text;
enihs 2008-10-25
  • 打赏
  • 举报
回复
还可以用存储过程 参数处理逻辑在存储过程里面做
止戈而立 2008-10-25
  • 打赏
  • 举报
回复
//少写了个AND
string sql="select * from yourtable where 1=1 {0}";
string subsql="";
if(combobox1.Text=="all")
subsql=" and stdid="+bombobox1.Text;
sql=string.Format(sql,subsql);
止戈而立 2008-10-25
  • 打赏
  • 举报
回复
string sql="select * from yourtable where 1=1 {0}";
string subsql="";
if(combobox1.Text=="all")
subsql="stdid="+bombobox1.Text;
sql=string.Format(sql,subsql);

110,556

社区成员

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

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

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