以下便是ASP.NET 多条件动态参数查询的方法,大家可以参考以下代码或直接复制使用哦。
protected void Button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder strWhere = new System.Text.StringBuilder();
strWhere.Append(" 1=1 ");//1=1全匹配,相当于没有条件
if (this.TextBox1.Text.Trim() != "")//第一个条件
{
strWhere.AppendFormat(" and Yuan_Name='{0}'", this.TextBox1.Text.Trim());
}
if (this.TextBox2.Text.Trim() != "")//第二个条件
{
strWhere.AppendFormat(" and Department_Name='{0}'", this.TextBox2.Text.Trim());
}
DataSet ds_Yuan = yuan.GetDataSet(strWhere.ToString());
this.GV_Yuan.DataSource = ds_Yuan.Tables["T_Yuan"];
this.GV_Yuan.DataBind();
this.lbl_Count.Text = ds_Yuan.Tables["T_Yuan"].Rows.Count.ToString();
Session["T_Yuan"] = ds_Yuan.Tables["T_Yuan"];
}