PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Building dynamic sql query with deafult characters

Reply

Building dynamic sql query with deafult characters

 
Thread Tools Rate Thread
Old 09-02-2006, 07:04 PM   #1
=?Utf-8?B?SGFyaQ==?=
Guest
 
Posts: n/a
Default Building dynamic sql query with deafult characters


Hi All,

What is the good way to handle a string which contains characters like
single quote (') or square bracket with LIKE ([]), while building a SQL
statement. Right now iam searching these characters in the string and adding
the escape chars for that.

eg 1: (This will fail to search)

String* searchVal = S"Test'quote";
String* str = S"";
str = String::Format(S"Select * from t1 where c1 = '{0}'", searchVal);

eg 2: (so i need to add a extra ' in the string)
String* searchVal = S"Test''quote";

Any other solutions for this

Hari



  Reply With Quote
Old 10-02-2006, 08:44 AM   #2
Frans Bouma [C# MVP]
Guest
 
Posts: n/a
Default Re: Building dynamic sql query with deafult characters

Hari wrote:

> Hi All,
>
> What is the good way to handle a string which contains characters
> like single quote (') or square bracket with LIKE ([]), while
> building a SQL statement. Right now iam searching these characters in
> the string and adding the escape chars for that.
>
> eg 1: (This will fail to search)
>
> String* searchVal = S"Test'quote";
> String* str = S"";
> str = String::Format(S"Select * from t1 where c1 = '{0}'",
> searchVal);
>
> eg 2: (so i need to add a extra ' in the string)
> String* searchVal = S"Test''quote";
>
> Any other solutions for this


never concat values into a query. Use a parameter like this:
str = S"SELECT * FROM t1 WHERE C1=@param";

and then create a new parameter:
SqlParameter* param = new SqlParameter();
// fill in parameter's properties here.
// add it to the SqlCommand object:
cmd.Parameters.Add(param);

FB


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
  Reply With Quote
Old 10-02-2006, 06:34 PM   #3
=?Utf-8?B?SGFyaQ==?=
Guest
 
Posts: n/a
Default Re: Building dynamic sql query with deafult characters

Thanks Frans, Also Iam is works if use 'LIKE' within a where clause? (Iam
not sure about using wild characters within a param object???)
eg.

SELECT * from t1 WHERE c1 LIKE @param

Hari


"Frans Bouma [C# MVP]" wrote:

> Hari wrote:
>
> > Hi All,
> >
> > What is the good way to handle a string which contains characters
> > like single quote (') or square bracket with LIKE ([]), while
> > building a SQL statement. Right now iam searching these characters in
> > the string and adding the escape chars for that.
> >
> > eg 1: (This will fail to search)
> >
> > String* searchVal = S"Test'quote";
> > String* str = S"";
> > str = String::Format(S"Select * from t1 where c1 = '{0}'",
> > searchVal);
> >
> > eg 2: (so i need to add a extra ' in the string)
> > String* searchVal = S"Test''quote";
> >
> > Any other solutions for this

>
> never concat values into a query. Use a parameter like this:
> str = S"SELECT * FROM t1 WHERE C1=@param";
>
> and then create a new parameter:
> SqlParameter* param = new SqlParameter();
> // fill in parameter's properties here.
> // add it to the SqlCommand object:
> cmd.Parameters.Add(param);
>
> FB
>
>
> --
> ------------------------------------------------------------------------
> Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
> My .NET blog: http://weblogs.asp.net/fbouma
> Microsoft MVP (C#)
> ------------------------------------------------------------------------
>

  Reply With Quote
Old 10-02-2006, 07:24 PM   #4
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
 
Posts: n/a
Default Re: Building dynamic sql query with deafult characters

Hari,

One way:

cmd.CommandText = "Select * From Students Where Name Like @Name"
cmd.Parameters.Add("@Name", "%John%")

Kerry Moorman


"Hari" wrote:

> Thanks Frans, Also Iam is works if use 'LIKE' within a where clause? (Iam
> not sure about using wild characters within a param object???)
> eg.
>
> SELECT * from t1 WHERE c1 LIKE @param
>
> Hari
>
>
> "Frans Bouma [C# MVP]" wrote:
>
> > Hari wrote:
> >
> > > Hi All,
> > >
> > > What is the good way to handle a string which contains characters
> > > like single quote (') or square bracket with LIKE ([]), while
> > > building a SQL statement. Right now iam searching these characters in
> > > the string and adding the escape chars for that.
> > >
> > > eg 1: (This will fail to search)
> > >
> > > String* searchVal = S"Test'quote";
> > > String* str = S"";
> > > str = String::Format(S"Select * from t1 where c1 = '{0}'",
> > > searchVal);
> > >
> > > eg 2: (so i need to add a extra ' in the string)
> > > String* searchVal = S"Test''quote";
> > >
> > > Any other solutions for this

> >
> > never concat values into a query. Use a parameter like this:
> > str = S"SELECT * FROM t1 WHERE C1=@param";
> >
> > and then create a new parameter:
> > SqlParameter* param = new SqlParameter();
> > // fill in parameter's properties here.
> > // add it to the SqlCommand object:
> > cmd.Parameters.Add(param);
> >
> > FB
> >
> >
> > --
> > ------------------------------------------------------------------------
> > Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
> > My .NET blog: http://weblogs.asp.net/fbouma
> > Microsoft MVP (C#)
> > ------------------------------------------------------------------------
> >

  Reply With Quote
Old 12-02-2006, 11:20 AM   #5
Alec MacLean
Guest
 
Posts: n/a
Default Re: Building dynamic sql query with deafult characters


"Hari" <Hari@discussions.microsoft.com> wrote in message
news:ED8DA845-592D-4CF0-8B6D-0ED0D18BB638@microsoft.com...
> Thanks Frans, Also Iam is works if use 'LIKE' within a where clause? (Iam
> not sure about using wild characters within a param object???)
> eg.
>
> SELECT * from t1 WHERE c1 LIKE @param
>
> Hari



>> never concat values into a query. Use a parameter like this:
>> str = S"SELECT * FROM t1 WHERE C1=@param";
>>


Even better, don't build query as a string at all and use a stored procedure
(assuming SQL Server of some flavour). The use of the parameters and stored
procedures is the recommended approach by MS to prevent risk of your app
being attacked by the SQL injection technique.

Assuming your searched column is a varchar or text datatype, in the stored
procedure, append the wildcard symbol like this:

create procedure dbo.usp_Wildcard_SELECT
@SearchValue VARCHAR(30)
AS

SELECT Col1, <Col2 , etc as required...>
FROM yourSourceTable
WHERE
Col1 LIKE @SearchValue + '%'
-- optional ordering output
ORDER BY Col1


The above example will always add the wildcard to the end of whatever string
value is passed in. This is a common approach for finding records by using
peoples surnames for example.


Al


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off