473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How dynamically create WHERE statement...

I have an advanced search box. The user can type in multiple words in the
box. Those words are then used in the WHERE clause against a Description db
field.

So these words: plumber carpenter electrician

Would essentially equate to: "WHERE (Description LIKE '%plumber%') OR
(Description LIKE '%carpenter%') OR (Description LIKE '%electrician%')"

Is there any easy way to dynamically create this WHERE clasue? I know how
to do it manually by code, but I didn't know if I had to manually parse the
tokens and construct the clause or if there was an easier way...

(I'm using MySQL.)

Thanks.
Nov 7 '08 #1
12 1389
"Bobby Edward" <bo***@nobody.comwrote in message
news:um**************@TK2MSFTNGP02.phx.gbl...
I have an advanced search box. The user can type in multiple words in the
box. Those words are then used in the WHERE clause against a Description
db field.

So these words: plumber carpenter electrician

Would essentially equate to: "WHERE (Description LIKE '%plumber%') OR
(Description LIKE '%carpenter%') OR (Description LIKE '%electrician%')"

Is there any easy way to dynamically create this WHERE clasue? I know how
to do it manually by code, but I didn't know if I had to manually parse
the tokens and construct the clause or if there was an easier way...
UNDER NO CIRCUMSTANCES do this!!! Your solution is absolutely wide open to
SQL Injection:
http://www.google.co.uk/search?sourc...L+Injection%22

Instead, allow users to select the occupation(s) they're interested in e.g.
by ticking checkboxes or some other technique - basically, anything to avoid
dynamic SQL...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #2


You can read my take on it here:
http://www.sqlservercentral.com/arti...rproblem/2283/

The Zero to N Parameter Problem

"Bobby Edward" <bo***@nobody.comwrote in message
news:um**************@TK2MSFTNGP02.phx.gbl...
>I have an advanced search box. The user can type in multiple words in the
box. Those words are then used in the WHERE clause against a Description
db field.

So these words: plumber carpenter electrician

Would essentially equate to: "WHERE (Description LIKE '%plumber%') OR
(Description LIKE '%carpenter%') OR (Description LIKE '%electrician%')"

Is there any easy way to dynamically create this WHERE clasue? I know how
to do it manually by code, but I didn't know if I had to manually parse
the tokens and construct the clause or if there was an easier way...

(I'm using MySQL.)

Thanks.

Nov 7 '08 #3
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:us**************@TK2MSFTNGP03.phx.gbl...
>
UNDER NO CIRCUMSTANCES do this!!! Your solution is absolutely wide open to
SQL Injection:
http://www.google.co.uk/search?sourc...L+Injection%22

Instead, allow users to select the occupation(s) they're interested in
e.g. by ticking checkboxes or some other technique - basically, anything
to avoid dynamic SQL...
I appreciate that very much Mark. But, what if I want the user to search
for ANY kind of word? It may not be something that I can list.

Can't I just clean up the string, such as by IGNORING the following
words/special characters when I create the WHERE:
DELETE
REMOVE
DROP
SELECT
UPDATE
INSERT
WHERE
*
%
;
..
etc....
Nov 7 '08 #4
"sloan" <sl***@ipass.netwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>

You can read my take on it here:
http://www.sqlservercentral.com/arti...rproblem/2283/

The Zero to N Parameter Problem
Thanks. I'll check it out! ;)
Nov 7 '08 #5

The url not super "dynamic". But it has a mechanism for parameters.

The previous post is very correct. SQL Injection will mess you up.

"Bobby Edward" <bo***@nobody.comwrote in message
news:et***************@TK2MSFTNGP05.phx.gbl...
"sloan" <sl***@ipass.netwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>>

You can read my take on it here:
http://www.sqlservercentral.com/arti...rproblem/2283/

The Zero to N Parameter Problem
Thanks. I'll check it out! ;)

Nov 7 '08 #6
"Bobby Edward" <bo***@nobody.comwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
I appreciate that very much Mark. But, what if I want the user to search
for ANY kind of word? It may not be something that I can list.

Can't I just clean up the string, such as by IGNORING the following
words/special characters when I create the WHERE:
DELETE
REMOVE
DROP
SELECT
UPDATE
INSERT
WHERE
Absolutely not! Please please read some of the articles in the Google search
I posted.

1=1--;
DECLARE @strSQL nvarchar(100)
SET @strSQL = 'P'+'R'+'I'+'N'+'T ''H'+'E'+'L'+'L'+'O'''
EXEC sp_executesql @strSQL
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 7 '08 #7
>
Absolutely not! Please please read some of the articles in the Google
search I posted.

1=1--;
DECLARE @strSQL nvarchar(100)
SET @strSQL = 'P'+'R'+'I'+'N'+'T ''H'+'E'+'L'+'L'+'O'''
EXEC sp_executesql @strSQL
I'm using strongly typed XSD datasets with MySql. I thought that simply
replacing all special characters and db words with nothing would suffice,
such as...

strSearch = txtSearchString.text.replace("+","") ' strip out special
characters
strSearch = strSearch.replace("*","") ' strip more
strSearch = strSearch.replace("..... ' keep stripping them out
strSearch = strSearch.replace("DROP","") ' remove db type words
strSearch = strSearch.replace("DELETE","") ' remove db type words
strSearch = strSearch.replace("SELECT","") ' remove db type words
etc etc etc

Then parse what's left using the remaining words/tokens.

Or, maybe I'm too simple minded and am not getting the point. I will do
some more research.

Thanks for your excellent input as usual Mark...
Nov 7 '08 #8
Bobby Edward wrote:
I have an advanced search box. The user can type in multiple words in
the box. Those words are then used in the WHERE clause against a
Description db field.

So these words: plumber carpenter electrician

Would essentially equate to: "WHERE (Description LIKE '%plumber%') OR
(Description LIKE '%carpenter%') OR (Description LIKE '%electrician%')"

Is there any easy way to dynamically create this WHERE clasue? I know
how to do it manually by code, but I didn't know if I had to manually
parse the tokens and construct the clause or if there was an easier
way...

(I'm using MySQL.)
This may not apply because you're using MySQL, but with SQL Server, you can
use parameterized queries. Parameterized queries allow you to build dynamic
SQL statements that are not susceptible to SQL Injection. You can add
multiple parameters to the command object allowing you to run queries such
as "where x or y or z". The code below is the basic idea ...

SqlCommand cmd = new SqlCommand();

SqlParameter param =
new SqlParameter("@Description1", SqlDbType.VarChar);
param.Value = "%" + "plumber" + "%";
cmd.Parameters.Add(param);

param =
new SqlParameter("@Description2", SqlDbType.VarChar);
param.Value = "%" + "carpenter" + "%";
cmd.Parameters.Add(param);

string Sql =
" SELECT SomeColumns " +
" FROM YourTable " +
" WHERE Description LIKE @Description1 " +
" OR Description LIKE @Description2; ";

SqlConnection conn =
new SqlConnection("your connection string");

cmd.CommandText = Sql;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
SqlDataReader sdr = cmd.ExecuteReader();

--
Ben
http://allben.net/

Nov 7 '08 #9
Thanks Ben. Nice code.

I have XSD strongly typed DataSets that I access thru my Business Layer
code. It accesses MySql but since it's strongly typed doesn't that mean
that I can use the same mechanism with MySql? I'll give it a try.

Thanks again! ;)
Nov 7 '08 #10
Or should I just use a FilterExpression against my objectdatasource?

"Bobby Edward" <bo***@nobody.comwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:us**************@TK2MSFTNGP03.phx.gbl...
>>
UNDER NO CIRCUMSTANCES do this!!! Your solution is absolutely wide open
to SQL Injection:
http://www.google.co.uk/search?sourc...L+Injection%22

Instead, allow users to select the occupation(s) they're interested in
e.g. by ticking checkboxes or some other technique - basically, anything
to avoid dynamic SQL...

I appreciate that very much Mark. But, what if I want the user to search
for ANY kind of word? It may not be something that I can list.

Can't I just clean up the string, such as by IGNORING the following
words/special characters when I create the WHERE:
DELETE
REMOVE
DROP
SELECT
UPDATE
INSERT
WHERE
*
%
;
.
etc....


Nov 7 '08 #11
A sincere advice. Never use concatenation of strings. Always use
Parameterized query. It takes less line of code and peace of mind from
security viewpoint..
I think, mysql can also be used with parameterized query, but syntax would
be different.
--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------
"Bobby Edward" <bo***@nobody.comwrote in message
news:um**************@TK2MSFTNGP02.phx.gbl...
>I have an advanced search box. The user can type in multiple words in the
box. Those words are then used in the WHERE clause against a Description
db field.

So these words: plumber carpenter electrician

Would essentially equate to: "WHERE (Description LIKE '%plumber%') OR
(Description LIKE '%carpenter%') OR (Description LIKE '%electrician%')"

Is there any easy way to dynamically create this WHERE clasue? I know how
to do it manually by code, but I didn't know if I had to manually parse
the tokens and construct the clause or if there was an easier way...

(I'm using MySQL.)

Thanks.

Nov 7 '08 #12
Searched for you how to use parameterised query with Mysql.

http://forums.asp.net/t/470457.aspx

--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------
"Vinay Khaitan" <vk******@gmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>A sincere advice. Never use concatenation of strings. Always use
Parameterized query. It takes less line of code and peace of mind from
security viewpoint..
I think, mysql can also be used with parameterized query, but syntax would
be different.
--
Vinay Khaitan
[Windows Forms Layout Control]
http://www.smart-components.com/
----------------------------------------------------------------
"Bobby Edward" <bo***@nobody.comwrote in message
news:um**************@TK2MSFTNGP02.phx.gbl...
>>I have an advanced search box. The user can type in multiple words in the
box. Those words are then used in the WHERE clause against a Description
db field.

So these words: plumber carpenter electrician

Would essentially equate to: "WHERE (Description LIKE '%plumber%') OR
(Description LIKE '%carpenter%') OR (Description LIKE '%electrician%')"

Is there any easy way to dynamically create this WHERE clasue? I know
how to do it manually by code, but I didn't know if I had to manually
parse the tokens and construct the clause or if there was an easier
way...

(I'm using MySQL.)

Thanks.


Nov 7 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Agoston Bejo | last post by:
I am looking for the PL/SQL equivalent of the VBScript Exec and/or Eval functions, i.e. I want to be able to dynamically create a statement, then execute it in the current PL/SQL context, e.g. ...
20
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now...
6
by: Ken Varn | last post by:
I want to add my own custom <STYLE> section in the <HEAD> section of my ASP.NET page within a custom control. Can someone tell me how I can have my custom control add tags to the <HEAD> section of...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
2
by: Andy Sutorius via DotNetMonster.com | last post by:
Hi, I remember in classic ASP when you had a webpage with a large number of textboxes and you needed to perform an update sql statement you could loop through all of the fields and dynamically...
1
by: keithb | last post by:
I have found that I must re-create dynamically added controls on every postback in order to find and access them programatically. The controls I am working with are inside a GridView control. When...
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
2
by: Suman | last post by:
Happy Friday everyone!!! I am working on a windows service and a C# application and needed some help with certain functionality. Please read through my issue below. Thanks! I have a windows...
2
by: jmarendo | last post by:
Hello, After reading through the "Table Basics - DOM - Refer to table cells" example at mredkj.com , I modified the code for my own purposes. In the modified version, I create a hyperlink and...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.