Results 1 to 7 of 7

Thread: [2008] using VB variables in SQL statements

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Resolved [2008] using VB variables in SQL statements

    I want to use VB variables in SQL code...
    Dim varCount As Integer = 3
    Dim sqlCmd As New SqlCommand("SELECT test FROM MyTable WHERE id = {varCount}")

    The sql syntax may be wrong, because I typed this by hand, but I want to know how I can use varCount in my SQL statement...

    Can anyone help me?

    Thanks
    Last edited by Icyculyr; Feb 3rd, 2008 at 03:54 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2008] using VB variables in SQL statements

    vb.net Code:
    1. Dim sqlCmd As New SqlCommand("SELECT test FROM MyTable WHERE id = " & varCount & ")"

  3. #3

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] using VB variables in SQL statements

    simple, lol...

    Thanks

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2008] using VB variables in SQL statements

    That is for a numeric value.

    FYI: If varcount was a string, or text value, it would look like this
    vb.net Code:
    1. Dim sqlCmd As New SqlCommand("SELECT test FROM MyTable WHERE id = '" & varCount & "')"

  5. #5

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] using VB variables in SQL statements

    Thanks

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

    Re: [2008] using VB variables in SQL statements

    While Hack's advice will do the job in this case and in others too, I have to advise following best practice and use parameters whenever inserting values into SQL statements:
    vb.net Code:
    1. Dim command As New SqlCommand("SELECT MyColumn FROM MyTable WHERE ID = @ID", connection
    2.  
    3. command.Parameters.AddWithValue("@ID", id)
    By using parameters you generally make your code more readable, less error-prone and more secure. In this case it may make no significant difference but I think it's good to do things the "proper" way all the time unless there's a genuine reason for doing otherwise.
    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

  7. #7

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] using VB variables in SQL statements

    Thanks jm, I'll do that.

    Cheers

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