I am trying to Select data from MySQL database using VB.NET
Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand
conn.ConnectionString = "Server=localhost; user id=root; password=; database=aplikasi_store_testing;"
cmd.Connection = conn
conn.Open()
Dim Number As Integer
cmd.CommandText = "SELCECT nama_student FROM student where Id_student ='" & id & "'"
but i dont know how to put selected query into variable, anybody can help me ?
The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.
The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.
Mysql also supports the concept of User-defined variables, which allows passing of a value from one statement to another. A user-defined variable in Mysql is written as @var_name where, var_name is the name of the variable and can consist of alphanumeric characters, ., _, and $.
Dim StrVar as String
Dim rd As MySqlDataReader
Dim cmd as New MySqlcommand
cmd.commandtext = "Select student_name from student_table where student_id = @ID"
cmd.connection = conn
rd = cmd.ExecuteReader
if rd.read then
StrVar = rd.GetString(1)
end if
rd.close
Using the Data Reader it will let you assign the result of the query to your variable StrVar and this will come in handy. I use GetString because I assume it is a string type and GetValue for integer. The value "1" represent the column you want to pass to your variable.
Let me know if this works. Cheers..Happy Coding..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With