日期:2014-05-16  浏览次数:20395 次

怎样实现多条件搜索,求帮忙啊~~
这是我做的页面,和后台代码,怎样才能实现多条件搜索,但是不用书写太多的SQL语句,按照我的后天代码,四个条件,如果任选几个条件搜索的话,可能要写十多条SQl语句。求大神帮忙!!
------解决方案--------------------
加一个默认条件1=1,之后用键值对吧。 遍历键值对。来控制查询条件。 
------解决方案--------------------
貌似Name是个关键字啊,看来你这个数据库字段设计的不太好,上面的修改下:

select 你那几个列我就不写了 
from Student where (StudentID=''or StudentID=@StudentID)
and ([Name]='' or [Name]=@[Name])

------解决方案--------------------
五个裤衩连多条件都没做过吗?

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind("");
        }
    }
    protected void bind(string condition)
    {
        string sql = "select * from table where 1=1 " + condition;
    }
    protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
        string condition = "";
        if (!string.IsNullOrEmpty(Textid.Text))
        {
            condition += " and studentid =" + Textid.Text + "%'";//用+=
        }
        if (!string.IsNullOrEmpty(TextName.Text))
        {
            condition += " and name like '%" + TextName.Text + "%'";
        }
        //下面条件省略
    }

------解决方案--------------------
引用:
五个裤衩连多条件都没做过吗?

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind("");
        }
    }
    protected void bind(string condition)
    {
        string sql = "select * from table where 1=1 " + condition;
    }
    protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
        string condition = "";
        if (!string.IsNullOrEmpty(Textid.Text))
        {