Results 1 to 9 of 9

Thread: [2008] Using variables in an SQL string

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    80

    [2008] Using variables in an SQL string

    It is possible to use a variable in the SQL string below?
    Because the string is in quotes, I didn't know if it would recognize it as a variable.

    Just for some background, 'bfguide' is the name of the database, 'rate' is the name of the table, and 'count' is the name of a field in the table.

    VB Code:
    1. myConnection = New MySqlConnection("server=localhost; user id=admin; password=******; database=bfguide; pooling=false;")
    2.  
    3.  --->   strSQL = "UPDATE rate SET count =  **variable** WHERE ID = 1;"
    4.  
    5.         myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
    6.         myDataSet = New DataSet()
    7.         myDataAdapter.Fill(myDataSet, "bfguide")
    8.  
    9.         MySQLDataGrid.DataSource = myDataSet
    10.         MySQLDataGrid.DataBind()
    Last edited by nickf77; Mar 3rd, 2009 at 09:11 AM.

  2. #2
    Member Redeye44us's Avatar
    Join Date
    Apr 2003
    Posts
    50

    Re: [2008] Using variables in an SQL string

    I am somewhat of a newby, but in the past, I have done it like this:


    strSQL = "UPDATE rate SET count = " + variable + " WHERE ID = 1;"

    I break up the string into parts and insert the variables in between. The variable will need to be converted to a string (variable.tostring).

    Hope this helps!
    Last edited by Redeye44us; Mar 3rd, 2009 at 10:18 AM.

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

    Re: [2008] Using variables in an SQL string

    You will also need to put brackets around count....COUNT is a reserved word in SQL. If you have the ability, I would rename that field to something else to avoid any potential problems.

  4. #4
    Hyperactive Member kxcntry99's Avatar
    Join Date
    Jun 2006
    Location
    Pennsylvania
    Posts
    342

    Re: [2008] Using variables in an SQL string

    One point to keep in mind is that if you pull any values from a user entered text field you open yourself to sql injection attacks. I.e. " Update mytable set username = '" + txtuser.text + "' where id = '1'".

    The way to prevent this and help with readability of the string is to use paramteters. Depending on what db you are using they syntax would differ but something similar to this:
    Code:
    dim objsql_str as sqlcommand
    objsql_str.cmdtext ( Update Mytable set username = @username where id = '1')
    
    Dim paramUser as parameter
    paramUser.value = txtuser.text
    objsql_str.parameters.add(paramUser)
    There is much documentation here on the forums. This method has really helped me to cleanup my sql strings and make sure that I am not open to injection since a parameter is seen as text only and escape characters are not used as commands.

    Hope this helps
    Microsoft Office Integration:Useful Database Links:
    Connection Strings


    Im a pogramar
    Iam a programer
    I’m a programor

    I write code!

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] Using variables in an SQL string

    And.... (I'm surprised Hack didn't mention this) if you are doing Inserts, Updates or Delete queries (also known as Action Queries) you should use ExecuteNonQuery and not the Fill method.... this is because action queries don't return a proper resultset, so there's no telling what you'll get.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Member Redeye44us's Avatar
    Join Date
    Apr 2003
    Posts
    50

    Re: [2008] Using variables in an SQL string

    So much to learn! So little time!

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    80

    Re: [2008] Using variables in an SQL string

    Yeah, wow I completely overlooked the "UPDATE rate SET count = " & variable & "WHERE ID = 1" method. I've been using VB for quite some time now, and have used that before; I don't know why it didn't come to me earlier.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    80

    Re: [2008] Using variables in an SQL string

    Quote Originally Posted by techgnome
    And.... (I'm surprised Hack didn't mention this) if you are doing Inserts, Updates or Delete queries (also known as Action Queries) you should use ExecuteNonQuery and not the Fill method.... this is because action queries don't return a proper resultset, so there's no telling what you'll get.

    -tg
    What is it exactly that you're saying? I'm new with SQL and just copied and pasted that code from the web, so what should I change for the UPDATE part?

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] Using variables in an SQL string

    What I'm saying is that to update data in the database, do not use the .Fill method. Fill is used to get data out of the database (as the result of a SELECT). ExecuteNonQuery means "Execute this SQL, and I'm not expecting any data back" .

    I suggest starting with this: http://www.vbforums.com/showthread.php?t=337051 it's our home-grown FAQ and tutorial for VB & databases... people have had success starting with that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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