Results 1 to 4 of 4

Thread: [2008] - LIKE Query

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    [2008] - LIKE Query

    How would you do a like query? This is currently what I have, but it doesn't work, I get an error.

    Error Message:
    Public member 'Read' on type 'Integer' not found.

    vb Code:
    1. Dim connection As New OleDbConnection(dbQueryString)
    2. Dim command As New OleDbCommand("select * from info where `@sType` like @search order by `Title`", connection)
    3.  
    4. command.Parameters.AddWithValue("@sType", searchFilter.Text)
    5. command.Parameters.AddWithValue("@search", searchBox.Text & "%")
    6. connection.Open()
    7. Dim dr = command.ExecuteNonQuery()
    8. While dr.Read()
    9.        ' Some code
    10. End While
    I am still very new to VB.NET, so I have MANY questions

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] - LIKE Query

    There's nothing wrong with your query. The problem is here:
    vb.net Code:
    1. Dim dr = command.ExecuteNonQuery()
    You're calling ExecuteNonQuery, which returns an Integer, rather than ExecuteReader, which returns an OleDbDataReader. This is EXACTLY why you should NOT use type inference when declaring variables. I STRONGLY suggest that, if you use type inference at all, that you ONLY ever use it when you're assigning a literal value to the variable. Better you simply don't use it at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    Re: [2008] - LIKE Query

    1. what is type inference?

    2. Any reason after changing to ExecuteReader, it doesn't go into the while loop? If i do "a" (without quotes), it doesn't find anything in my database, even though the word "Animals" (without quotes) is in there.
    I am still very new to VB.NET, so I have MANY questions

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] - LIKE Query

    1. Open your MSDN Library, type in the term and see.

    2. Your query is never going to work. I didn't notice to begin with because I wasn't looking for it but you're trying to specify a column name using a parameter. That is not possible. Identifiers like table and column names must be specified explicitly. Always use parameters to insert values into an SQL statement, as you are, but you have to use string concatenation to insert a variable table or column name.

    What you should do is populate a ComboBox with a list of valid column names. If you know what they are at design time then you can hard code them. If you don't then you can use the GetSchema method of an OleDbConnection object to get the information. The user will then select a known value from a list, rather than being able to type any old thing into a TextBox. As such there's no need for you to validate the value they enter. You can then concatenate the column name into the SQL string:
    vb.net Code:
    1. Dim command As New OleDbCommand("select * from info where `" & searchCombo.SelectedValue & "` like @search order by `Title`", connection)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width