Results 1 to 36 of 36

Thread: DB Search Command

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    DB Search Command

    I've got a searh form with about 8 different boxes where a user can enter data to search from. To save writing many different search commands I want to be able to write one and if the field is empty then search all records

    for example

    SELECT * FROM MyTable WHERE CarType = CarType.Text And Color = Color.Text

    If a user enters a Color then search it otherwise don't narrow down the results so like this

    SELECT * FROM MyTable WHERE CarType = ""

    How can I do this? would it be like the % sine if Color.Text is = String.Empty??

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: DB Search Command

    Let's suppose you have 3 fields in your DB (Field1, Field2 and Field3)
    And you have 3 textboxes on your form and a Submit button.

    Then we can design something like this.

    Code:
    Dim sql, condition1, condition2, condition3 As String
    
    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
         If TextBox1.Text = "" Then 
              condition1 = "(TRUE)" 
         Else
              condition1 = String.Format("Field1='{0}')", TextBox1.Text)    
         End If
    End Sub
    
    Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
         If TextBox1.Text = "" Then 
              condition2 = "(TRUE)" 
         Else
              condition2 = String.Format("Field2='{0}')", TextBox2.Text)    
         End If
    End Sub
    
    Private Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
         If TextBox1.Text = "" Then 
              condition3 = "(TRUE)" 
         Else
              condition3 = String.Format("Field3='{0}')", TextBox3.Text)    
         End If
    End Sub
    
    Private Sub btnSumbit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        sql = "SELECT * FROM MyTable WHERE " _
                    & condition1 & " AND " _
                    & condition2 & " AND " _
                    & condition3 & ";"
      
         ' Submit your sql
    
    End Sub
    If all textboxes are empty then your sql will look like:
    Code:
    SELECT * FROM MyTable WHERE (TRUE) AND (TRUE) AND (TRUE);

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: DB Search Command

    A better idea is to not check anything at all, by not putting it in to the SQL statement in the first place.

    You can see a VB6 example (so wrong syntax, but the right idea) in the "Further Steps" link in my signature.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: DB Search Command

    that link does not work

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: DB Search Command

    What do you mean by "does not work"? Are you getting an error message of some sort when you click on it?

    I just tested in two browsers (even when logged out) and it worked fine in both.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: DB Search Command

    It directs me here http://www.microsoft.com/visualstudi...al-studio-2010

    can you post the full URL for me

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: DB Search Command

    Did you click the adword link in my post, rather than the "Further Steps" link in my signature I told you about?

    The URL for it is: http://www.vbforums.com/showthread.php?t=551154

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: DB Search Command

    I know how to add more Fild = 'blaa blaa' but when there is more than one WHERE how can I add the AND inbetween?

    For example
    Code:
    Dim strSQL As String, strWhere As String = ""
    strSQL = "SELECT * FROM MyTable"
    
    If MeMe.Text <> "" Then strWhere = strWhere & " WHERE MeMe= '" & MeMe.Text & "'"
    If Options.Text And strWhere <> "" Then strWhere = strWhere & " Type = '" & Options.Text & "'" Else strWhere = strWhere & "AND Type = '" & Options.Text & "'"
    
    Dim NewSQL As String = ""
    If strWhere <> "" Then
    NewSQL = strSQL & " WHERE " & strWhere
    End If

  9. #9

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

    Re: DB Search Command

    Quote Originally Posted by cicatrix View Post
    USE the example I showed you.
    Never use string concatenation to build SQL statements if it can possibly be avoided. E.g.
    vb.net Code:
    1. Dim query As String = "SELECT * FROM Table1 WHERE @Column1 = '' OR Column1 = @Column1 AND @Column2 = '' OR Column2 = @Column2"
    2. Dim command As New SqlCommand(query, connection)
    3.  
    4. command.Parameters.AddWithValue("@Column1", Me.TextBox1.Text.Trim())
    5. command.Parameters.AddWithValue("@Column2", Me.TextBox2.Text.Trim())
    This is one option so that an empty TextBox will match every row. Alternatively you could write some conditional code that won't add the parameters at all for empty controls. No point handling the TextChanged events though. Just get all the values once when you execute the query.
    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

  11. #11
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: DB Search Command

    Quote Originally Posted by jmcilhinney View Post
    Never use string concatenation to build SQL statements if it can possibly be avoided.
    OK. What can possibly go wrong with the code I provided?

    Well, I admit that a user can use characters ' or " in the textbox and exploit some SQL injection but this should be done by filtering user input. What's wrong with the concatenation itself?

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

    Re: DB Search Command

    Quote Originally Posted by cicatrix View Post
    OK. What can possibly go wrong with the code I provided?

    Well, I admit that a user can use characters ' or " in the textbox and exploit some SQL injection but this should be done by filtering user input. What's wrong with the concatenation itself?
    You just described the downside. What's the upside? There isn't one, so why not just do it the right way? That way you're safe from the problems you anticipate and those you don't.
    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

  13. #13

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

    Re: DB Search Command

    Quote Originally Posted by cicatrix View Post
    I still can't see how your example in the post #10 gets around the SQL injection risk.
    The difference is that your code is processed on the client, while mine is processed on the server. You are inserting literal values into the SQL code first, then sending that SQL code to the server. Let's say that you have this code:
    vb.net Code:
    1. Dim query = "SELECT * FROM SomeTable WHERE SomeColumn = '" & someTextBox.Text & "'"
    If the user then types the following into that TextBox:
    Code:
    '; DELETE FROM SomeTable; SELECT * FROM SomeTable WHERE SomeColumn = '
    That literal string will be inserted into your SQL code before sending it to the server, so the actual SQL code you execute will be:
    Code:
    SELECT * FROM SomeTable WHERE SomeColumn = ''; DELETE FROM SomeTable; SELECT * FROM SomeTable WHERE SomeColumn = ''
    Congratulations! You just deleted every record in your table courtesy of SQL injection.

    Now, if you had used a parameter:
    vb.net Code:
    1. Dim query = "SELECT * FROM SomeTable WHERE SomeColumn = @SomeColumn"
    then that is the SQL that would be sent to the server. The server would then compare the value of the parameter, i.e. the value the user entered, to the values in the SomeColumn column. That value is treated as text rather than executed as SQL code, so no harm can be done.
    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

  15. #15

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: DB Search Command

    The important thing is that the items inside Parameters can only ever be treated as data - which not only means that it is safer, but you also don't need to have code to validate that the input (such as dealing with the ' character, etc).

    You also don't need to worry about formatting values within the SQL statement (such as forcing dates to US or ISO formats, and numbers to use . as the decimal point), because that is all dealt with in other ways by the Parameters.

    While Parameters are certainly a very good idea in any situation (including this one), they don't need to exclude string building in general - arguably the best way to deal with this situation is to build the conditions in the string (with placeholders for the parameters), and then add the actual values with Parameters.
    Quote Originally Posted by AirlineSim View Post
    I know how to add more Fild = 'blaa blaa' but when there is more than one WHERE how can I add the AND inbetween?
    There should not be more than one Where, you clearly didn't pay enough attention to the examples in the link I gave (one of which contains multiple conditions).

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: DB Search Command

    Could I not do this
    Code:
    Dim Search as String = "&#37;"
    If Textbox1.Text =String.Empty Then
    Search = "%"
    Else
    Search = Textbox1.Text
    End If
    Wont the % sign find anything??

  18. #18
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DB Search Command

    Of course not if there is a &#37; you need to use a LIKE search not an equal serach

    Where FileName LIKE '%'
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  19. #19
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: DB Search Command

    ..and it also won't work unless the field is a text based data type.

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: DB Search Command

    Well I have this and it does not work when nothing is entered into textbox1.text

    Code:
    Dim BDLocation As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Users.mdb"
                Dim connection As New OleDb.OleDbConnection(BDLocation)
                connection.Open()
                Dim command As New OleDb.OleDbCommand("SELECT * FROM Users WHERE Username = '' Or Username = @data", connection)
                command.Parameters.AddWithValue("@data", TextBox1.Text.Trim())
    
                Dim reader As OleDb.OleDbDataReader = command.ExecuteReader
                While reader.Read
                    ListBox1.Items.Add(reader("Username"))
                End While
                connection.Close()

  21. #21
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DB Search Command

    Can UserName ever have an empty string in it. And remeber that NULL data is not the same as ''
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: DB Search Command

    Quote Originally Posted by GaryMazzone View Post
    Can UserName ever have an empty string in it. And remeber that NULL data is not the same as ''
    Yes it can.

  23. #23
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DB Search Command

    So UserName is not NULL but can have an empty string...

    If there are empty strings stored there then this (run right in an Access Query) will bring back records right?

    SELECT * FROM Users WHERE Username = ''

    Does it?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: DB Search Command

    I'm wanting it to return all records if left blank. The username field will always be filled in.

  25. #25
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DB Search Command

    Is the textfield is Blank the the statement needs to be:

    SELECT * FROM Users WHERE Username LIKE '&#37;'

    Or

    SELECT * FROM USERS
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  26. #26
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: DB Search Command

    Quote Originally Posted by jmcilhinney View Post
    If the user then types the following into that TextBox:
    Code:
    '; DELETE FROM SomeTable; SELECT * FROM SomeTable WHERE SomeColumn = '

    I think the odds of a user knowing or guessing the table name to type in would be pretty low, though I guess it's possible...

  27. #27
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DB Search Command

    First query:

    '; Select Table_Name from Information_Schema.Tables

    New query
    '; Drop Table One of the names you just got back from the first.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  28. #28
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: DB Search Command

    Quote Originally Posted by GaryMazzone View Post
    First query:

    '; Select Table_Name from Information_Schema.Tables

    New query
    '; Drop Table One of the names you just got back from the first.

    Are these universal names or database specific?

  29. #29
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: DB Search Command

    They are universal... but only for the database systems that support it. Other database systems have their own equivalents, but they are very east to find.

    You might be thinking of ways to stop it without Parameters (such as blocking the ' character), but that is far from safe (there are "secret" character combinations that work too).


    If you don't want to allow injection attacks, use Parameters.

    If you want to make your code simpler (eg: no checking for special characters, no formatting of values within SQL statements), use Parameters.


    There is more info about the benefits in the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of the Database Development forum), and via the link in jmcilhinney's signature.

  30. #30

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: DB Search Command

    Quote Originally Posted by GaryMazzone View Post
    Is the textfield is Blank the the statement needs to be:

    SELECT * FROM Users WHERE Username LIKE '%'

    Or

    SELECT * FROM USERS
    but as it's been said it only works for string, what about numbers?

  31. #31
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: DB Search Command

    I'm not disputing the benefits of using parameters ... I'm just wondering how common sql injection attacts are in the real world. I'm guessing that web apps would be more vulnerable than internal apps, unless you had some disgruntled employees...

  32. #32
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DB Search Command

    There is not wildcard seaching on anything but text fields. That means no number fields, date fields, time fields or DateTime fields
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  33. #33
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: DB Search Command

    Code:
    "SELECT * FROM Users WHERE Username = '' Or Username = @data"
    This SQL will return only records where the username field is empty or where the username field contains the text from the textbox.

    I think that I can combine string building and parameters into something safe:

    Here's my previous example with modification:
    Let's suppose you have 3 fields in your DB (Field1, Field2 and Field3)
    And you have 3 textboxes on your form and a Submit button.

    vb.net Code:
    1. Dim SQLcmd As New OleDb.OleDbCommand
    2. Dim sql, condition1, condition2, condition3 As String
    3.  
    4. Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    5.      If TextBox1.Text = "" Then
    6.           condition1 = "(TRUE)"
    7.      Else
    8.           condition1 = "(Field1=@arg1)"
    9.           SQLcmd.Parameters.AddWithValue("arg1", Textbox1.Text)
    10.      End If
    11. End Sub
    12.  
    13. Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    14.      If TextBox1.Text = "" Then
    15.           condition2 = "(TRUE)"
    16.      Else
    17.           condition2 = "(Field2=@arg2)"  
    18.           SQLcmd.Parameters.AddWithValue("arg2", Textbox2.Text)
    19.      End If
    20. End Sub
    21.  
    22. Private Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    23.      If TextBox1.Text = "" Then
    24.           condition3 = "(TRUE)"
    25.      Else
    26.           condition3 = "(Field3=@arg3)"    
    27.           SQLcmd.Parameters.AddWithValue("arg3", Textbox3.Text)
    28.      End If
    29. End Sub
    30.  
    31. Private Sub btnSumbit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    32.      SQLcmd.CommandText = "SELECT * FROM MyTable WHERE " _
    33.                 & condition1 & " AND " _
    34.                 & condition2 & " AND " _
    35.                 & condition3 & ";"
    36.  
    37.      ' Submit your sql
    38.      ' SAFE from injections!
    39.      
    40. End Sub

    In your case "SELECT * From Users WHERE (TRUE) AND (TRUE) AND (TRUE);" will be an equivalent of "SELECT * FROM Users;"

  34. #34
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: DB Search Command

    Rather than setting the parameters etc in the _TextChanged events (which could run multiple times and therefore add multiple parameters for each, or add them in the wrong order), I would recommend doing it all when the SQL statement is about to run (inside btnSumbit_Click).

    As an added bonus, all of the code for this can be in one place - which makes it much easier to debug etc.

  35. #35
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: DB Search Command

    Yes, I didn't consider that. Of course you shouldn't add the parameters in the TextChanged event. Everything can (and should) be done in a single procedure:


    vb.net Code:
    1. Private Sub btnSumbit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    2.      Dim SQLcmd As New OleDb.OleDbCommand
    3.      Dim condition1, condition2, condition3 As String
    4.  
    5.      If TextBox1.Text = "" Then
    6.           condition1 = "(TRUE)"
    7.      Else
    8.           condition1 = "(Field1=@arg1)"
    9.           SQLcmd.Parameters.AddWithValue("arg1", Textbox1.Text)
    10.      End If
    11.  
    12.      If TextBox1.Text = "" Then
    13.           condition2 = "(TRUE)"
    14.      Else
    15.           condition2 = "(Field2=@arg2)"  
    16.           SQLcmd.Parameters.AddWithValue("arg2", Textbox2.Text)
    17.      End If
    18.  
    19.      If TextBox1.Text = "" Then
    20.           condition3 = "(TRUE)"
    21.      Else
    22.           condition3 = "(Field3=@arg3)"    
    23.           SQLcmd.Parameters.AddWithValue("arg3", Textbox3.Text)
    24.      End If
    25.  
    26.  
    27.      SQLcmd.CommandText = "SELECT * FROM MyTable WHERE " _
    28.                 & condition1 & " AND " _
    29.                 & condition2 & " AND " _
    30.                 & condition3 & ";"
    31.  
    32.      ' Submit your sql
    33.      ' SAFE from injections!
    34.      
    35. End Sub

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

    Re: DB Search Command

    Quote Originally Posted by si_the_geek View Post
    While Parameters are certainly a very good idea in any situation (including this one), they don't need to exclude string building in general - arguably the best way to deal with this situation is to build the conditions in the string (with placeholders for the parameters), and then add the actual values with Parameters.
    That's a very good point. I said:
    Never use string concatenation to build SQL statements
    when what I really meant was never use string concatenation to insert literal column values. I've posted some code in the CodeBank that builds up a SQL statement using concatenation but still uses parameters to create an IN clause.
    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