在C#中查询数据库。但是条件是可变的。where 后面跟的一个或多个条件,
因为条件个数不确定,所以语句不知道怎么写  求高手教我!!!QQ348771523

解决方案 »

  1.   

    sql = "select * from table where ";sql += "x = 1 and ";
    sql += "y = 'hello' and ";
    sql += "z < 20 and ";
    ...sql += "1 = 1";
      

  2.   

    string strSQL = "Select * From 表"
    if(条件A)
      strSQL += 语句A
    else if(条件B)
      strSQL += 语句B
      

  3.   

    1楼的方法再稍微改进一下更好:sql = "select * from table where 1=1 ";sql += " and x = 1 ";
    sql += " and y = 'hello' ";
    sql += " and z < 20 ";
    ...这样,即使where条件一个都没有都不会出错。
      

  4.   

    就是动态查询,以前是sql拼接字符串,象楼上的一样,但用在WEB上要注意SQL注入
    用linq的话可以用表达式树
      

  5.   

    sql="select * from table where datetimes1=@datetime1 and datetime2=@datetime2"
    query.Parameters.Add("@datetime1", SqlDbType.Date);
    query.Parameters["@datetime1"].Value = dateTimepicker.Value;
    query.Parameters.Add("@datetime2", SqlDbType.Date);
    query.Parameters["@datetime2"].Value = dateTimepicker1.Value;