Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Creating SQL commands

  1. #1

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Resolved [RESOLVED] [2005] Creating SQL commands

    I'm just trying to get the hang of SQL and .NET. I was wondering can I use the field, from a textbox for example, to help create my SQL command?

    This wont work so obviously there's a problem with doing it this way:

    vb Code:
    1. ' Set the SELECT statement for the Command object
    2.             cmdUserSelect.CommandText = "SELECT ShapeID, " _
    3.                 & "Shape, Colour " _
    4.                 & "FROM Features WHERE Shape = " & Me.shape.Text

    Am I close or miles away?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  2. #2
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] Creating SQL commands

    I'm assuming shape is a textbox then?
    If so then yeah you'll be able to do that but remember that any strings need to be put in single quotes.

    Hope this helps
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

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

    Re: [2005] Creating SQL commands

    Also if doing it this way use a Replace to replace any single qoutes with 2 single qoutes.

    vb Code:
    1. cmdUserSelect.CommandText = "SELECT ShapeID, " _
    2.         & "Shape, Colour " _                
    3.         & "FROM Features WHERE Shape = '" & Me.shape.Text.Replace("'","''").Trim() & "'"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: [2005] Creating SQL commands

    If it's a string field, you need single quotes around it.

    vb Code:
    1. cmdUserSelect.CommandText = "SELECT ShapeID, " _                & "Shape, Colour " _
    2.     & "FROM Features WHERE Shape = '" & Me.shape.Text & "'"


    Also, read this MSDN article on SQL Injection to learn why you have to be very careful about appending user supplied strings into SQL statements.

  5. #5
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] Creating SQL commands

    You could also look into sql parameters because they get rid of having to do any of that.

    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  6. #6

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Creating SQL commands

    Thanks guys. Getting there slowly but surely.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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