-
Jan 15th, 2016, 05:29 AM
#1
Thread Starter
Addicted Member
vb.net sql query with variable
hi, i've an access database where i store my data, an i want search all field that contain a keyword so do in my vb.net code query like this
Access.ExecQuery("SELECT distinct field2 FROM Data WHERE (field1='" + field1Selected + "') AND (field2 contains '" + txtFindKeyWord.Text + "') ORDER BY field1")
lstfield1.Items.Clear()
For Each R As DataRow In Access.DBDT.Rows
lstfield1.Items.Add(R("field1"))
FindResult = FindResult + 1
but not work; if i replace field2 contains '" + txtFindKeyWord.Text + "' with a costant string value ito work, but i want to chose which keyword i need to find. Anyone can help?
-
Jan 15th, 2016, 07:52 AM
#2
Re: vb.net sql query with variable
Originally Posted by hannibal smith
hi, i've an access database where i store my data, an i want search all field that contain a keyword so do in my vb.net code query like this
Access.ExecQuery("SELECT distinct field2 FROM Data WHERE (field1='" + field1Selected + "') AND (field2 contains '" + txtFindKeyWord.Text + "') ORDER BY field1")
lstfield1.Items.Clear()
For Each R As DataRow In Access.DBDT.Rows
lstfield1.Items.Add(R("field1"))
FindResult = FindResult + 1
but not work; if i replace field2 contains '" + txtFindKeyWord.Text + "' with a costant string value ito work, but i want to chose which keyword i need to find. Anyone can help?
well first off, the SQL isn't close to being right.
Field1 will only return if there's an exact match, and field2 ... well... what you want is a LIKE clause, and you'll need to include the wildcard characters too. And lastly you'll only get records where BOTH conditions are met. Is that what you want or do you want where either OR the other is true?
-tg
-
Jan 15th, 2016, 03:37 PM
#3
Thread Starter
Addicted Member
Re: vb.net sql query with variable
Originally Posted by techgnome
well first off, the SQL isn't close to being right.
Field1 will only return if there's an exact match, and field2 ... well... what you want is a LIKE clause, and you'll need to include the wildcard characters too. And lastly you'll only get records where BOTH conditions are met. Is that what you want or do you want where either OR the other is true?
-tg
in my database field1 is a list of brand, and field2 are models of products; i would find all products name of a selectedbrand that contain a keyword(input with textbox) in the name.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|