根据输入的搜索条件返回指定的sql字符串(多条件搜索)
来源:互联网 发布:应届毕业生找工作知乎 编辑:程序博客网 时间:2024/05/17 19:19
using System;using System.Collections.Generic;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) { } protected void Button1_Click(object sender, EventArgs e) { string Name = this.TextBox1.Text; string Age = this.TextBox2.Text; string Hobby = this.TextBox3.Text; string Address = this.TextBox4.Text; this.TextBox5.Text = GetWhere(Name, Age, Hobby, Address); } /// <summary> /// 根据输入的条件返回指定的sql字符串 /// </summary> /// <param name="Name"></param> /// <param name="Age"></param> /// <param name="Hobby"></param> /// <param name="Address"></param> /// <returns></returns> public string GetWhere(string Name, string Age,string Hobby,string Address) { string Condition = " where 1=1 ";//初始化 if (Name != "") { Condition += " and Name = '" + Name+ "' "; } if (Age != "") { Condition += " and Age= '" + Age + "' "; } if (Hobby != "") { Condition += " and Hobby= '" + Hobby+ "' "; } if (Address != "") { Condition += " and Address= '" + Address + "' "; } string SelectString = "select * from Table " + Condition + " order by Name"; return SelectString; }}