您可以使用 ExecuteScalar 方法如下
object nama_studentObj = cmd.ExecuteScalar()
if nama_studentObj != null then
string nama_student= nama_studentObj .ToString()
完整的示例代码
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
笔记 :
调用数据库时最好使用参数,如下所示
cmd.CommandText = "SELCECT nama_student FROM student where Id_student = @Id_student"
那么你必须将参数添加为
cmd.Parameters.AddWithValue("Id_student", id )
如何创建参数化 SQL 查询?我为什么要?