首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

:当有多个查询条件可供用户输入的时候,如何根据用户输入了哪些就根据哪些进行查询呢

2013-01-22 
求助:当有多个查询条件可供用户输入的时候,怎么根据用户输入了哪些就根据哪些进行查询呢?如题:当有多个查

求助:当有多个查询条件可供用户输入的时候,怎么根据用户输入了哪些就根据哪些进行查询呢?
如题:
当有多个查询条件可供用户输入的时候,怎么根据用户输入了哪些就根据哪些进行查询呢?
比如说:
数据表中有姓名,性别,年龄,地址,电话五个字段
aspx页面有五个Textbox,对应这五个条件
用户可以一个都不输入,那咱么就默认为查询所有的.
可以随意输入其中几个,也可以全都输入
那我们在后台怎么根据用户的选择进行相应的查询呢?
笨办法是用if判断,但要是查询条件太多,那得多少个if啊?
哪位朋友指点一下,谢谢啦!
[解决办法]
string sql = "select * from tt where 1=1";
if (this.Textbox1.Text!="")
  sql+=" and 字段1= 'this.Textbox1.Text'" ;
else
  sql+="";

if (this.Textbox2.Text!="")
  sql+=" and 字段2= 'this.Textbox2.Text'" ;
else
  sql+="";


大概就这种格式吧

[解决办法]
同你一楼的图设计方法,你不可能实现搜索到全部记录。
只能要么是“男”或是要么“女”的记录。因此此下拉列表,还得有一个全选的项目。

Refer:
http://www.cnblogs.com/insus/articles/1999795.html


[解决办法]
 List<SqlParameter> ParList = new List<SqlParameter>();
            string str = "select * from 表 where 1=1";
            if (textBox1.Text.Trim() != "")
            {
                str += " and name=@name";
                ParList.Add(new SqlParameter("@name", textBox1.Text.Trim()));
            }
            if (textBox2.Text.Trim() != "")
            {
                str += " and age=@age";
                ParList.Add(new SqlParameter("@age", textBox2.Text.Trim()));
            }
            ……
            查询(str, ParList);
[解决办法]


StringBuilder str = new StringBuilder();
            if (txtCheckStartDate.Text != string.Empty && txtCheckEndDate.Text != string.Empty)
            {
                str.Append("and checkInDate between '" + txtCheckStartDate.Text + "' and '" + txtCheckEndDate.Text + "'");
            }
            else if (txtExprieStartDate.Text != string.Empty && txtExprieEndDate.Text != string.Empty)
            {
                str.Append("and exprieDate between '" + txtExprieStartDate.Text + "' and '" + txtExprieEndDate.Text + "'");


            }
            if (txtOldPeopleName.Text != string.Empty)
            {
                str.Append(" and userName like '%" + txtOldPeopleName.Text + "%'");
            }
            if (cboIsArrearage.Checked)
            {
                str.Append(" and isArrearage=" + 1);
            }
            if (cboSocialSecurity.Checked)
            {
                str.Append(" and SocialSecurity=" + 1);
            }
            if (cboIsChronicDisease.Checked)
            {
                str.Append(" and isChronicDisease=" + 1);
            }
            return str.ToString();

热点排行