//姓名不为空就把姓名加入where条件
if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
{
sql = sql + string.Format(" and name like '%{0}%'", this.txtName.Text.Trim())
}
//身份z号不为空就把身份z号加入where条件
if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
{
sql = sql + string.Format(" and id like '%{0}%'", this.txtID.Text.Trim())
}
这个很简单...用一个string类型记录用户的选择,然后动态添加相应的查询条件..
string sqlstr="select * from table where 1=1 "
if 用户选择一 then
sqlstr=sqlstr+" and conditonn1 "
if 用户选择二 then
sqlstr=sqlstr+" and condition2"
....
等等 以此类推
当然你也可以用存储过程,那样相对比较安全..
用(+)来实现, 这个+号可以这样来理解: + 表示补充,即哪个表有加号,这个表就是匹配表。所以加号写在左表,右表就是全部显示,故是右连接。SQL>Select * from dave a,bl b where a.id(+)=b.id
ID NAME ID NAME
---------- ---------- ---------- ----------
1 dave1 dave
2 bl 2 bl
1 bl 1 dave
2 dave2 bl
3 dba 3 big bird
4 sf-express 4 exc
9 怀宁
也可以用right outer join实现同样的功能:
SQL>select * from dave a right outer join bl b on a.id = b.id
ID NAME ID NAME
---------- ---------- ---------- ----------
1 dave1 dave
2 bl 2 bl
1 bl 1 dave
2 dave2 bl
3 dba 3 big bird
4 sf-express 4 exc
9 怀宁 --此处左表不足用Null 填充
已选择7行。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)