SQL Statement???

D

Darryn Ross

Hi,

I am trying to execute a SQL statement that has the 'LIKE' condition in it,
i am reading from a access 2002 database and when i build my statement i am
trying to include the '%' wildcard and it's not picking it up.... my select
statement is as follows....

SqlSelect = "Select * From tbl Where Code LIKE " + "'%" + txtcode.Text +
"%'" ;

after the line is executed the percent characters are missing??? am i using
the wrong syntax?? i am pretty sure this is the way it's done...

Regards

Darryn
 
J

Jon Skeet [C# MVP]

Darryn Ross said:
I am trying to execute a SQL statement that has the 'LIKE' condition in it,
i am reading from a access 2002 database and when i build my statement i am
trying to include the '%' wildcard and it's not picking it up.... my select
statement is as follows....

SqlSelect = "Select * From tbl Where Code LIKE " + "'%" + txtcode.Text +
"%'" ;

after the line is executed the percent characters are missing??? am i using
the wrong syntax?? i am pretty sure this is the way it's done...

Firstly, it would be better to use a parameter and make the value of
that parameter "%"+txtcode.Text+"%" to avoid SQL injection attacks.

Beyond that - what's the value of SqlSelect after you've created it? It
looks okay to me...
 
D

Darryn Ross

Hi Jon,

I tried the parameter but it wouldn't return anything again??? this is
really strange! i changed my statement to look like this....

string sqlCode ;
sqlCode = "'*" + txtcode.Text + "*'" ;

SqlSelect = "Select * From tbl Where Code LIKE " + sqlCode ;

i changed the % to a * because access acknowledges the * instead of the % as
a wildcard for the LIKE condition!! don't know why they didn't make the same
wildcards for all their database querying languages???

the query runs great from access but as soon as i run the same command from
code it plays up.

Regards

Darryn
 
J

Jon Skeet [C# MVP]

Darryn Ross said:
I tried the parameter but it wouldn't return anything again??? this is
really strange! i changed my statement to look like this....

string sqlCode ;
sqlCode = "'*" + txtcode.Text + "*'" ;

SqlSelect = "Select * From tbl Where Code LIKE " + sqlCode ;

Other than the stars, that certainly won't make any difference. When I
talked about parameters, I meant as in OleDbParameter etc.
i changed the % to a * because access acknowledges the * instead of
the % as a wildcard for the LIKE condition!! don't know why they
didn't make the same wildcards for all their database querying
languages???

No idea - I personally don't like access as a database, but there we
go...
the query runs great from access but as soon as i run the same command from
code it plays up.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
C

Chris R. Timmons

Hi,

I am trying to execute a SQL statement that has the 'LIKE'
condition in it, i am reading from a access 2002 database and
when i build my statement i am trying to include the '%'
wildcard and it's not picking it up.... my select statement is
as follows....

SqlSelect = "Select * From tbl Where Code LIKE " + "'%" +
txtcode.Text + "%'" ;

after the line is executed the percent characters are missing???
am i using the wrong syntax?? i am pretty sure this is the way
it's done...

Darryn,

Is the Code column a text type, or numeric? Wildcards generally only
work with text columns.

Here are some links from MSDN that might help:

http://search.microsoft.com/search/results.aspx?view=msdn&st=a&na=81&
qu=access+wildcard&qp=&qa=&qn=&c=10&s=1

or

http://tinyurl.com/6b8ym
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top