Uma Shankar Patel
Where to use 1=1 condition in where clause?
By Uma Shankar Patel in .NET on Aug 29 2013
  • Pankaj  Kumar Choudhary
    May, 2015 14

    This method mainly used in injection ............

    • 0
  • Uma Shankar Patel
    Aug, 2013 29

    If you don't know  the list of conditions at compile time and it will built at run time, Then you can made a condition with “where 1=1”. and for other conditions that will affect run time, use

    and .

    Example

    StringBuilder sb = new StringBuilder();
    sb.Append("SELECT * FROM Products");  // Your query
    sb.Append(" WHERE 1=1"); // always true condition
    // append query's where clause
    if (catID != 0)
    {
    sb.Append(" AND categoryID= {0}", catID);
    }
    if (minPrice > 0)
    {
    sb.Append(" AND itemPrice >= {0}", minPrice);
    }
    SqlCommand cmd = new SqlCommand(sb.ToString(), cnn);
    SqlDataReader dr = cmd.ExecuteReader();
    // your code to read data from dr.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS