比如我在数据库里有一张a表,里面有两个时段name和age,age是1,2,3,4.100现在我用下拉框年龄的范围,那我得到下拉框的值,怎么在数据库里怎么查询

解决方案 »

  1.   

      <select name="age" id="age">
      <option value="1=1">不限</option>
      <option value="age>=0 and age<=15">0-15岁</option>
      <option value="age>=16 and age<=22">16-22岁</option>
      <option value="age>=23 and age<=30">23-30岁</option>
      <option value="age>=31 and age<=40">31-40岁</option>
      <option value="age>=41">41-长生不老</option>
      </select>后台
    string SQL = "SELECT * FROM a表 WHERE " + 这个参数;
      

  2.   

    LZ的意思是SQL怎么查询? ...where age < xx and age>xx
      

  3.   

    下拉框的value可以设置成这样0-15,16-22,23-30,31-40,40---测试数据
    create table o(name varchar(10),age int)
    insert into o select 'aa',23
    insert into o select 'bb',4
    insert into o select 'cc',34
    insert into o select 'dd',63
    insert into o select 'ee',15
    go
    create proc GetPeople
    @ages varchar(20)
    as
    select * from o where age between left(@ages,charindex('-',@ages)-1) and
    (case right(@ages,len(@ages)-charindex('-',@ages)) when '' then 999999999 else right(@ages,len(@ages)-charindex('-',@ages)) end)
    go--测试结果:
    exec GetPeople '0-15'
    exec GetPeople '40-'
      

  4.   

    或者使用二楼的思路。
    查询语句直接使用:string sql="select * from 表名 where "+下拉框选定值
      

  5.   

    我是用三层写的string SQL = "SELECT * FROM a表 WHERE " + 这个参数;这个我放在数据访问层的,在后代应该怎么得到这个值啊
        protected void Select4_ServerChange(object sender, EventArgs e)
        {
            string abc=this.Select4.SelectedIndex.ToString();点不出selectedvalue.tostring();
        }
      

  6.   

    SELECT * FROM a WHERE age BETWEEN 0 AND 15