|
-
Mar 3rd, 2009, 09:04 AM
#1
Thread Starter
Lively Member
[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:
myConnection = New MySqlConnection("server=localhost; user id=admin; password=******; database=bfguide; pooling=false;")
---> strSQL = "UPDATE rate SET count = **variable** WHERE ID = 1;"
myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New DataSet()
myDataAdapter.Fill(myDataSet, "bfguide")
MySQLDataGrid.DataSource = myDataSet
MySQLDataGrid.DataBind()
Last edited by nickf77; Mar 3rd, 2009 at 09:11 AM.
-
Mar 3rd, 2009, 10:07 AM
#2
Member
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.
-
Mar 3rd, 2009, 11:25 AM
#3
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.
-
Mar 3rd, 2009, 11:42 AM
#4
Hyperactive Member
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! 
-
Mar 3rd, 2009, 12:01 PM
#5
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
-
Mar 3rd, 2009, 12:13 PM
#6
Member
Re: [2008] Using variables in an SQL string
So much to learn! So little time!
-
Mar 3rd, 2009, 03:41 PM
#7
Thread Starter
Lively Member
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.
-
Mar 3rd, 2009, 03:44 PM
#8
Thread Starter
Lively Member
Re: [2008] Using variables in an SQL string
 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?
-
Mar 3rd, 2009, 04:55 PM
#9
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|