Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select from mysql put into variable VB.NET

Tags:

mysql

vb.net

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 ?

like image 382
Vinra Gunanta Pandia Avatar asked Apr 16 '13 02:04

Vinra Gunanta Pandia


People also ask

How do I assign a selection to a variable in mysql?

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.

What is select * from in mysql?

The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.

Can you use variables in mysql?

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 $.


1 Answers

 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..

like image 99
Androidz Avatar answered Oct 03 '22 17:10

Androidz