如何从id =随机数的数据库中检索数据和显示?

时间:2017-03-27 16:52:32

标签: sql vb.net

尝试显示我的数据库中id等于先前生成的数字的数据,不幸的是我收到了一个错误,该错误在where子句'中显示了未知列q1。数据库表有3列,idtopic1,问题,答案。感谢任何帮助。谢谢。

这是我的代码:

    Private Sub ViewBtn_Click(sender As Object, e As EventArgs) Handles ViewBtn.Click
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString =
    "server=localhost;userid=root;password=root99;database=database"
    Dim READER As MySqlDataReader
    Dim rndnum As Random
    Dim q1 As Integer
    rndnum = New Random
    q1 = rndnum.Next(1,5)
    Dim q1display As String

    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "select question from database.topic1 where idtopic1= q1"
        COMMAND = New MySqlCommand(Query, MysqlConn)
        READER = COMMAND.ExecuteReader
        q1display = Convert.ToString(COMMAND.ExecuteScalar())
        q1box.Text = q1display

        MysqlConn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()
    End Try
   End Sub
   End Class

1 个答案:

答案 0 :(得分:2)

更改以下行:

Query = "select question from database.topic1 where idtopic1= q1"

要:

Query = "select question from database.topic1 where idtopic1= " & q1