Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 SQL Command Misbehaving under VStudio 2005

Author  Topic 

Marc_CAT
Starting Member

2 Posts

Posted - 2008-08-25 : 04:32:32
Hello to everyone!

I'm developing a C# application that manages an Access database. In order to perform a word search within the database registers, I have used a SQL command based on the 'LIKE' condition. I attach the complete code below:

                
myConnection.Open();
OleDbDataAdapter adapt_pregunta = new OleDbDataAdapter();
string query_c = String.Format("SELECT * FROM PREGUNTA WHERE TEMA LIKE '*{0}*' OR NUM_TEMA LIKE '*{1}*' OR AUTOR LIKE '*{2}*' OR ENUNCIAT LIKE '*{3}*'", text, text, text,text);
OleDbCommand comm_preg = new OleDbCommand(query_c, myConnection);
adapt_pregunta.SelectCommand = comm_preg;
OleDbCommandBuilder cb_preg = new OleDbCommandBuilder(adapt_pregunta);
DataSet ds_preg = new DataSet("PREGUNTA");
adapt_pregunta.Fill(ds_preg, "PREGUNTA");
DataTable dt_preg = ds_preg.Tables[0];

myConnection.Close();


Where the final SQL command is:
SELECT * FROM PREGUNTA WHERE TEMA LIKE '*3*' OR NUM_TEMA LIKE '*3*' OR AUTOR LIKE '*3*' OR ENUNCIAT LIKE '*3*'

Although the command works properly if I manually insert it as a new query in MS Access, it does not work with the previous code. The DataTable is not filled.

I have tried a simple "SELECT * FROM PREGUNTA" command so to check if it was a code problem, and surprisingly, it works well.

Does anybody know where the problem is?

Thanks.


Marc

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-08-25 : 05:26:04
Sure you want the LIKE '*3*',

Should that be LIKE '%3%' instead? (are you trying to match any record with a 3 in it?


-------------
Charlie
Go to Top of Page

Marc_CAT
Starting Member

2 Posts

Posted - 2008-08-25 : 05:49:58
Hi!
It works!

What I do is to check the query under MS Access before writing it in my code. And although I had read that LIKE condition needed '%', I later found out that Access' SQL does not use the command "LIKE '%'", but "LIKE '*'".
Now, after replacing them within the code, C# executes the query correctly and therefore, my doubt is solved.

Thanks a lot!
Go to Top of Page
   

- Advertisement -