组合查询

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;//查询按钮
        if (IsPostBack == false)
        {
            Repeater1.DataSource = new CarData().SelectAll();
            Repeater1.DataBind();
        }
    }
    void Button1_Click(object sender, EventArgs e)
    {
        Hashtable hs = new Hashtable();
        int ccc = 0;
        string Tsql = "select *from car";
        if (TextBox1.Text != "")
        {
            Tsql += " where name like @name ";
            hs.Add("@name", "%" + TextBox1.Text + "%");
            ccc++;
        }
        if (TextBox2.Text != "")//不为空进行拼接
        {
            if (ccc > 0)
            {
                Tsql += " and powers like @pow";
            }
            else
            {
                Tsql += " where powers like @pow";
            }
            hs.Add("@pow", "%" + TextBox2.Text + "%");
            ccc++;
        }
        if (TextBox3.Text != "")//第三个框为空
        {
            if (ccc > 0)
            {
                Tsql += " and price " + DropDownList1.SelectedItem.Value + " @pri";
            }
            else
            {
                Tsql += " where price " + DropDownList1.SelectedItem.Value + " @pri";
            }
            hs.Add("@pri", TextBox3.Text);
            ccc++;
        }
        Label1.Text = Tsql;
        Repeater1.DataSource = new CarData().Select(Tsql,hs);
        Repeater1.DataBind();
    }
   
}

 

posted @ 2016-07-26 05:42  一人饮酒醉(SeVen❤)  阅读(151)  评论(0编辑  收藏  举报