|
-
Jul 31st, 2010, 04:55 AM
#1
Thread Starter
New Member
Select statement that uses a variable, result to a text box
Hi,
I'm relatively new to VB.
I want to perform a SELECT statement that uses a variable to return a value, example;
mysql> SELECT room_description FROM room_descriptions WHERE current_room = 'someVariable';
and display the result in a TextBox i.e. TextBox.text = someVariable.
So how do I perform a query from VB that passes a variable?
How do I assign the result to a variable?
Also, is there an excellent book or web site the demonstrates all these things in simple examples?
Many thanks,
Ted.
-
Jul 31st, 2010, 06:05 AM
#2
Re: Select statement that uses a variable, result to a text box
Welcome to the forums. 
Change myConnection to whatever you are using as your connection object.
SqlCommand and SqlDataReader are only valid for SQL Server. If that is what you are using, then you are good. If not, then they need to be changed as appropriate.
Code:
Dim sSQL As String
sSQL = "SELECT room_description "
sSQL = sSQL & "FROM room_descriptions "
sSQL = sSQL & "WHERE current_room = @Room "
Dim command As New SqlCommand(sSQL, myConnection)
command.Parameters.AddWithValue("@Room", txtRoom.Text)
Dim reader As SqlDataReader = command.ExecuteReader()
reader.Read()
TextBox1.Text = reader.GetString(0)
reader.Close()
-
Jul 31st, 2010, 11:32 AM
#3
Re: Select statement that uses a variable, result to a text box
It would actually be better to call ExecuteScalar, which is intended for queries that return a single value, rather than ExecuteReader. The rest would be the same but this part:
Code:
Dim reader As SqlDataReader = command.ExecuteReader()
reader.Read()
TextBox1.Text = reader.GetString(0)
would become:
Code:
TextBox1.Text = CStr(command.ExecuteScalar())
-
Jul 31st, 2010, 12:48 PM
#4
Thread Starter
New Member
Re: Select statement that uses a variable, result to a text box
Hi,
many thanks to you both for the replies.
I'll give this a go and get back to you.
Cheers,
Ted.
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
|