TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Interviews
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
Java
Learn iOS Programming
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
Where to use 1=1 condition in where clause?
By
Uma Shankar Patel
in
.NET
on
Aug 29
2013
0
1.9k
2
Post Your Answer
Submit
May, 2015
14
This method mainly used in injection ............
0
Aug, 2013
29
If you don't know the list of conditions at compile time and it will built at run t
ime, 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
Developer (2375)
Programmer (1288)
Sr. Developer (827)
Sr. Programmer (449)
Fresher (398)
Tech Lead (376)
DBA (244)
Team Lead (187)
Project Lead (83)
Architect (28)
View All
MOST LIKED QUESTIONS
Why do you want to leave your current company?
What are the advantages of using REST in Web API?
What is ASP.NET Core?
How to iterate through ArrayList in jQuery?
What is the difference between TempData keep() and peek() function?
A class provides a default constructor for me. I write a constructor that takes a string as...
Can multiple catch blocks be executed in a C# program?
what is Sealed class
What were your responsibilities in your previous job ?
What Operating Systems Node.js Supports