.net多條件查詢

  string str = "";
        string sql ="";
        if (this.DropDownList1.SelectedItem.Text.Trim() != "請選擇")       
        {
            str += " and ";
            str += "zname=" + "'" + this.DropDownList1.SelectedItem.Text.Trim() + "'";        
        }
        if (this.DropDownList2.SelectedItem.Text.Trim() != "請選擇")
        {
            str += " and ";
            str += "zqs=" + "'" + this.DropDownList2.SelectedItem.Text.Trim() + "'";
        }
        if (this.DropDownList3.SelectedItem.Text.Trim() != "請選擇")
        {
            str += " and ";
            str += "zyear=" + "'" + this.DropDownList3.SelectedItem.Text.Trim() + "'";
        }
        string str1 = "";

        if (str.StartsWith(" and"))
        {           
            str1 = str.Substring(5);
        }
        if (str1 == "")
        {
            sql = "select * from zztable";
        }
        else
        {
            sql = "select * from zztable where " + str1.ToString().Trim();
        }
        DataTable dt = obj.GetTable(sql);
        GridView1.DataSource = dt;
        GridView1.DataBind();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章