设要查找的内容为ID,Name,Score,字段名也一样,则
string strSQL = "Select * From [User] Where 1 = 1";
if (ID > 0)
{
  strSQL += " And [ID] = " + ID.ToString();
}
if (Name != string.Empty)
{
  strSQL += " And [Name] = '" + Name + "'";
}
if (Score > 0)
{
  strSQL += " And [Score] = " + Score.ToString();
}

解决方案 »

  1.   

    设要查找的参数为@ID,@Name,@Score,字段名也一样,则
    string strSQL = "Select * From [User] Where (ID='"+@ID+"'or '"+@ID+"'='') and (Name='"+@Name+"'or '"+@Name+"'='') and (Score='"+@Score+"'or '"+@Score+"'='')";
    一条语句就可以了
      

  2.   

    StringBuilder sql=new StringBuilder ();
    sql="select * from aa where ";
    if(dropdownlist1.selectedvalue!=0)
    {
       sql.Append(dropdownlist1.selectedItem.text).append(" and");
    }
    ....依次写
    dropdownlist1...就是你的多选条件框了
      

  3.   

    自己去拼查询语句
    sqlQuery = "";
    if (ID!="")
    sqlQuery += "ID = '" + txtID.Text +"'"
    if (Name != "")
    {
    if(sqlQuery == "")
      sqlQuery += "Name = '" + txtName.Text +"'"
    else
      sqlQuery += " and Name = '" + txtName.Text +"'"
    }
    ...后面有多少条件就写多少判断去拼
    if(sqlQuery != "")
     sqlQuery = "where" + sqlQuery;最后再连上你前面的查询语句
      

  4.   

    用存储过程:声明参数 @name varchar(10)=null
    使用参数:where  name=@name or name is null
      

  5.   

    支持2楼的方法,我是这样做的,你看会不会适合。
    public DataTable GetData(int id, int score, string name, int scoreType )
    {
       DataTable targetTable = new DataTable();
       string selectSql = "";
       string strWhere = " where  1=1 ";
       if(id > 0)
          strWhere += " and id=" + id.ToString();
       if(score > 0)
       {
          switch(scoreType)
          {
             case 0:
                strWhere += " and score=" + score.ToString();
                break;
             case 1:
                strWhere += " and score>" + score.ToString();
                break;
             case 2:
                strWhere += " and score<" + score.ToString();
                break;
             default:            
                break;
          }
       }   if(name != null && name.length > 0)
          strWhere += " and name=" + name;   selectSql = "select * from tableName  " + strWhere;   //  执行数据库检索
       return targetTable;
    }调用的时候如果不按编号或成绩查询可以将相应字段设置为-1, 不查询姓名时可以设置为“”或null。如: DataTable  table1 = GetData(-1, -1, "小王", 0);
         DataTable  table2 = GetData(-1, 60, null, 1);