you can use the ExecuteScalar method as below
object nama_studentObj = cmd.ExecuteScalar() if nama_studentObj != null then string nama_student= nama_studentObj .ToString()
Full code example
Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim stm As String = "SELECT VERSION()" Dim version As String Dim conn As MySqlConnection Try conn = New MySqlConnection(cs) conn.Open() Dim cmd As MySqlCommand = New MySqlCommand(stm, conn) version = Convert.ToString(cmd.ExecuteScalar()) Console.WriteLine("MySQL version: {0}", version) Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) Finally conn.Close() End Try
Note:
It is better to use parameters when calling the database, for example below
cmd.CommandText = "SELCECT nama_student FROM student where Id_student = @Id_student"
then you should add the parameter as
cmd.Parameters.AddWithValue("Id_student", id )
How to create a parameterized SQL query? Why should I?
source share