MYSQL Select Command with variable as field name

What is the syntex to select records from a mysql database using a variable as the field name.
i.e. select * from database where myvariable = '" & "test" & "'   the myvariable will be a field name selected by the user from a drop down list.
whiwexAsked:
Who is Participating?
 
pivarConnect With a Mentor Commented:
Hi,

I suppose you're talking about creating sql statement in VB? Is it this you're looking for?

"select * from database where " & myvariable & " ='" & Combobox1.Text & "'"

/peter
0
 
pdd1lanCommented:
have you tried like below

select * from database where myvariable ='" & Combobox1.Text & "'"
0
 
whiwexAuthor Commented:
yes it treats myvariable as a fieldname so it errors to unknown column name myvariable
I need it to treat the contents of the myvariable as the field name
0
 
CodeCruiserCommented:
Well I think it would be

MyCommand.Text = "select * from database where " & combobox1 & " ='" & txtSearch.Text & "'"


where MyCommand is the MySQL command object in VB.NET
0
 
whiwexAuthor Commented:
thanks that was it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.