473,473 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Like Operator Problem

3 New Member
I am writing code to search for books where some string is within the book's name

it works fine when i set the search parameter to the whole name of the book but when i use the % sign wildcard no results seem to generate

this is my code :

Expand|Select|Wrap|Line Numbers
  1. OleDbConnection myConn = null;
  2. string ConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;User Id=;Password=;Data Source=Library.mdb";
  3.  
  4. myConn = new OleDbConnection(ConnString);
  5.  
  6. string OleDb = "SELECT Nom,Auteur,DateEdition  FROM Livres WHERE Nom Like '%bookname%'";
  7.  
  8. OleDbCommand cmd = new OleDbCommand(OleDb,myConn );
  9. cmd.Parameters.Add("@bookname", txtfind.Text);
  10.  
  11. dt = new DataTable();
  12. OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
  13. ad.Fill(dt);
  14. dgsearchbooks.DataSource = dt; 
  15.  
i know it's something in the sql query i just cant seem to figure it out.
May 23 '10 #1
5 8598
incinerator
3 New Member
SOLVED

string OleDb = "SELECT Nom,Auteur,DateEdition FROM Livres WHERE Nom Like '%"+txtfind.Text+"%'";
May 23 '10 #2
Plater
7,872 Recognized Expert Expert
If that wasn't the solution to it, maybe it could be this:
In MS access you use the * as the wildcard and not the % like SQLServer
May 25 '10 #3
incinerator
3 New Member
i am Indeed using an ms access database but using the % wildcard sign worked for me , but thanks for your reply, appreciated.
May 25 '10 #4
Christian Binder
218 Recognized Expert New Member
I think, the problem was that you've escaped the parameter and missed the @-sign.

Expand|Select|Wrap|Line Numbers
  1. string OleDb = "SELECT Nom,Auteur,DateEdition FROM Livres WHERE Nom Like '%bookname%'";
  2. // ...
  3. cmd.Parameters.Add("@bookname", txtfind.Text);
  4.  
Right:


Expand|Select|Wrap|Line Numbers
  1. string OleDb = "SELECT Nom,Auteur,DateEdition FROM Livres WHERE Nom Like '%' + @bookname + '%'";
  2. // ...
  3. cmd.Parameters.Add("@bookname", txtfind.Text);
  4.  
OR (apply the percent-signs on parameter addition)

Expand|Select|Wrap|Line Numbers
  1. string OleDb = "SELECT Nom,Auteur,DateEdition FROM Livres WHERE Nom Like @bookname";
  2. // ...
  3. cmd.Parameters.Add("@bookname", "%" + txtfind.Text + "%");
  4.  
May 26 '10 #5
Curtis Rutland
3,256 Recognized Expert Specialist
The last thing you want to do is remove the parameter altogether from your query. That leaves you vulnerable to SQL injection attacks. Always use parameterized queries.
May 28 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Vlad | last post by:
I operate with 2 tables: tbLocations and tbDepartments. Both have a column named Town. tbLocations.Town looks like long unique list of different towns world wide: Copenhagen Madrid Roma
10
by: Steve Jorgensen | last post by:
Microsoft acknowledges a bug with this, but the bug is worse than they say. It turns out that if you use the Like operator without a wildcard on a snapshot type recordset, all sorts of wierd,...
4
by: Regnab | last post by:
I've got a number of 'Lots' of cattle - ie 40A, 372A, W123 etc. The letter in the Lot number indicates where the animal is traceable or not. The animal's Lot number, along with its individual...
6
by: Saradhi | last post by:
I want to check for a conditon as follows: if ( name Like "sa*") So, I need to use this * as a wild card and the above statement should return all those names which starts with sa. I know...
2
by: Ed Brown | last post by:
I'm working on a VB.Net application that needs to do quite a bit of string pattern matching, and am having problems using the "LIKE" operator to match the same string twice in the pattern. For...
6
by: rocksoft | last post by:
Hi, i'm using mysql database, i don't know syntax how to pass the parameter in like operator inside the stored procedure, i want to select the store number related to textbox input number...
2
by: neeraj | last post by:
Hi, all Could anny one give me help how can I use like operator with these data types "integer , datetime or boolean" in DataView.RowFilter Actually when I try to get the data from dataview...
2
by: Deepa Jeevagan | last post by:
Hi, The application I am currently working on is developed in Oracle 8i. I have a table which has a nullable numeric column say CustNo. This column has an index defined on it and oracle uses...
1
MindBender77
by: MindBender77 | last post by:
Hello All, I've always had problems with the "like" operator in a query especially using the wildcard "*". This is my latest issue. I have a query that produces 4 fields if the Line Tech field is...
7
by: bytesshalini | last post by:
Hi friends... can anybody tell me how to use the like operator to fetch only digits... for eg: i have a col x whose value is 7890. i want to use like operator in select stmt to fetch the value of...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.