我的意思是,在做查询,因为有多个查询条件,不可能每个都必须选择一个数据,所以就想当某个条件不选择时就查询出所有的记录
我的意思是,在做查询,因为有多个查询条件,不可能每个都必须选择一个数据,所以就想当某个条件不选择时就查询出所有的记录
你的意思是不是有好多查询条件
但是你有时候只会选择其中的某个条件来查询
达到查询出你所想要的条件的查询结果啊???
做个判断就行了吧
StringBuilder sb = new StringBuilder();
sb.Append(" 1=1 ");
if (txtdeviceContractNo.Text.Trim().ToString() != string.Empty)
{
sb.Append(" and ContractNoSys like '%" + txtdeviceContractNo.Text.Trim().ToString() + "%' ");
}
if (txtprojectNo.Value.Trim().ToString() != string.Empty)
{
sb.Append(" and ProjectNo like '%" + txtprojectNo.Value.Trim().ToString() + "%'");
}
if (ddlcontractStatus.SelectedValue.Trim().ToString() != string.Empty && ddlcontractStatus.SelectedValue != "请选择")
{
sb.Append(" and ContractStatusCode='" + ddlcontractStatus.Text.Trim().ToString() + "'");
}
//sb.ToString()就是你想要的查询条件
[IMG]F:\1112.jpg[/IMG]
看看我的查询条件
代码如下:
string sqlstr = "select ID,NAME,AGE,JERQUEDATE,HOSPITAL,CLINIC,JC_DOCTOR,MACHINE,MIRRORIMAGE,DIAGNOSES,PATHOLOGY,SALARY from patientMsg where 1=1";
if(this.txtName.Text !="")
{
sqlstr += "and";
sqlstr += " NAME like '" + this.txtName.Text + "%'";
}
if (this.DropHospital.SelectedItem.Text != "----请选择----")
{
sqlstr += "and";
sqlstr += " HOSPITAl='"+this.DropHospital.SelectedItem.Text+"'";
}
if (this.DropClinic.SelectedItem.Text != "----请选择----")
{
sqlstr += "and";
sqlstr += " CLINIC='" + this.DropClinic.SelectedItem.Text + "'";
}
if (this.DropJcDoctor.SelectedItem.Text != "----请选择----")
{
sqlstr += "and";
sqlstr += " JC_DOCTOR ='" + this.DropJcDoctor.SelectedItem.Text + "'";
}
if (this.DropSjDoctor.SelectedItem.Text != "----请选择----")
{
sqlstr += "and";
sqlstr += " SJ_DOCTOR ='" + this.DropSjDoctor.SelectedItem.Text + "'";
}
if (this.DropMachine.SelectedItem.Text != "----请选择----")
{
sqlstr += "and";
sqlstr += " MACHINE ='" + this.DropMachine.SelectedItem.Text + "'";
}
if (this.txtStarDate.Text != "" && this.txtEndDate.Text != "")
{
sqlstr += "and";
sqlstr += " JERQUEDATE between '" + this.txtStarDate.Text + "' and '" + this.txtEndDate.Text + "'";
}
else if (this.txtStarDate.Text != "" && this.txtEndDate.Text == "")
{
sqlstr += "and";
sqlstr += " JERQUEDATE between '" + this.txtStarDate.Text + "' and '" + DateTime.Today.ToShortDateString() + "'";
}
else if (this.txtStarDate.Text == "" && this.txtEndDate.Text != "")
{
sqlstr += "and";
sqlstr += " JERQUEDATE < '" + this.txtEndDate.Text + "'";
}
if (this.DropAge.SelectedItem.Text != "全部")
{
sqlstr += "and";
sqlstr += " AGE" + this.DropAge.SelectedItem.Text + " " + this.txtAge.Text + "";
}
SqlConnection conn =new SqlConnection(str);
conn.Open();
SqlDataAdapter ada = new SqlDataAdapter(sqlstr, conn);
DataSet set = new DataSet();
ada.Fill(set);
if (set.Tables[0].Rows.Count>0)
{
this.GridView1.DataSource = set;
this.GridView1.DataBind();
}
else
{
Response.Write("<script>alert('对不起,没有找到你所查询的病人资料!')</script>");
}
conn.Close();
你的也是如此,一样的道理