| Author |
Topic |
|
Nightmare
Starting Member
8 Posts |
Posted - 2006-12-27 : 15:09:03
|
| Hi all criteria = "Select * From Familias Where Print LIKE '"%text2%"'" Can someone help me with this cmd? The error is on "%Text2%" part.I dont know the sintax!Email: Nightmare.gmr@gmail.comMNS: Fjulr@hotmail.comThanks in advance |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-12-27 : 15:17:59
|
| Is that line of code from an application? The final SQL query should beSelect * From Familias Where Print LIKE 'abc%'The percent sign is a wild card, so that would give you all records that have the string abc... (starting with the letters abc, followed by anything) in them. |
 |
|
|
Nightmare
Starting Member
8 Posts |
Posted - 2006-12-27 : 16:47:38
|
| Yes its part of an Visual Basic application where "Text2" is a text box...If i put "Select * From Familias Where Print LIKE '%abc%'" it works.. but i need to read the value in that Text Box... Thanks for the answer |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-12-27 : 17:03:11
|
| OK, so if it's VB, then it would be like thiscriteria = "Select * From Familias Where Print LIKE '" & Text2.Text & "%'"Note that this is open to a serious security problem known as a SQL Injection Attack. You should rather use a paramter in the SQL code and set the value of the parameter in your VB code. That would be something likecriteria = "Select * From Familias Where Print LIKE @Text"cmd.CommandText = criteriacmd.Parameters.Add(New SqlParameter('@Text', Text2.Text & "%")) |
 |
|
|
Nightmare
Starting Member
8 Posts |
Posted - 2006-12-27 : 17:22:21
|
| Thanks again for answer...but still didnt working (no recordes found) I test with "Dreia" and there are 1 record. Its just a sintax error but i dont know what to do.Thanks |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-12-27 : 17:27:29
|
| So if you run this in Query Analyzer, do you get recordsSelect * From Familias Where Print LIKE 'Dreia%'And if you run it in the VB code and you type Dreia (no quotes) in the text box, you get no records? |
 |
|
|
Nightmare
Starting Member
8 Posts |
Posted - 2006-12-27 : 17:31:27
|
| yes |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-12-27 : 17:37:59
|
| OK, so if you put a breakpoint in your VB code right before it executes the SQL, what is the value of the command text? It should be exactly the same as the SQL statement that works in Query Analyzer, if it isn't then you have found your problem.It's not a syntax error because a syntax error would be caught by the VB compiler or by SQL Server - the SQL statement that is being executed is not like the one that works in QA. |
 |
|
|
Nightmare
Starting Member
8 Posts |
Posted - 2006-12-27 : 17:41:16
|
| Well i tried with this code: criteria = "Select * From Familias Where Print LIKE '" & Text2.Text & "%'" <---- No VB erros but no records found.With this one:criteria = "Select * From Familias Where Print LIKE @Text"cmd.CommandText = criteriacmd.Parameters.Add(New SqlParameter('@Text', Text2.Text & "%")) <--- VB give me a sintax error hereThanks |
 |
|
|
Nightmare
Starting Member
8 Posts |
Posted - 2006-12-27 : 17:42:13
|
| All code:Private Sub Command2_Click()Call dblidhjaWith arcriteria = "Select * From Familias Where Print LIKE @Text"cmd.CommandText = criteriacmd.Parameters.Add(New SqlParameter('@Text', Text2.Text & "%"))If .RecordCount >= 1 ThenText3.Text = !FamiliaText2.Text = ""End IfEnd WithEnd Sub |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-12-27 : 17:47:04
|
| That's not all the code - what is ar? You never execute the command so certainly there are no records returned.Are you using VB .NET or VB 6.0? The code I gave you won't work on VB 6.0, it's VB .NET code. |
 |
|
|
Nightmare
Starting Member
8 Posts |
Posted - 2006-12-27 : 17:52:13
|
| Thats it im using VB 6.0Thank you |
 |
|
|
Nightmare
Starting Member
8 Posts |
Posted - 2006-12-27 : 19:51:36
|
| criteria = "Select * From Familias Where Print Like " & "'%" & Text2 & "%'"Thanks for the help SnSQL |
 |
|
|
|