Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm having some confusion for the sql query in vb.net.
Below is the query I placed in the code, but it return no result.
When should use the single quotes and double quotes?
Please help....Thanks in advance.

Dim command As String = "SELECT * from T_TRACKING_" + DropDownListSite.SelectedValue + " where NAME IN ('" + name + "') AND ID IN( " + id + ") ORDER BY TIME"
Posted
Updated 17-Jun-13 22:37pm
v2
Comments
CPallini 18-Jun-13 4:37am    
Why are you using the IN clause (probably you don't need it)?

I would suggest changing your code to user Parameterized queries.

Try reading the following:
http://www.vistadb.net/tutorials/insertrows-vb.aspx[^]
http://visualbasic.about.com/od/learnvbnet/a/begdbapp9.htm[^]

Once you have had a read of them, if you are unable to convert your current approach to a Paramterized approach come back and ask for some help.
 
Share this answer
 
IN requires a list, and a list does not require quotes around it. You may need quotes around each individual item in the list if they are not numeric though:
Bad:
SQL
...NAME IN ('Joe,Mike')...

Good:
SQL
...NAME IN ('Joe','Mike')...



"OK.I get what you mean. Do you have any idea to change the following input to the mentioned sample?
Input: Joe,Mike
Output: 'Joe','Mike'"



There are a number of ways to do it, from the simple "brute-force-and-ignorance" method:
VB
Dim s As String = "joe,Mike"
Dim t As String = "'" & s.Replace(",", "','") & "'"

Which works, but any spaces in the input string may cause problems.
A better method uses Linq methods:
VB
Dim s As String = "joe,Mike"
Dim t As String = String.Join(",", s.Split(","C).[Select](Function(f) "'" & f.Trim() & "'"))
 
Share this answer
 
v2
Comments
HL22 18-Jun-13 4:55am    
OK.I get what you mean. Do you have any idea to change the following input to the mentioned sample?
Input: Joe,Mike
Output: 'Joe','Mike'
OriginalGriff 18-Jun-13 5:10am    
Answer updated
HL22 18-Jun-13 5:15am    
hi,thank a lots. Problem solved.
OriginalGriff 18-Jun-13 5:23am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900