多次单击一个按钮和mySql

时间:2014-12-30 17:48:58

标签: mysql vb.net button

我正在制作一种发电机。每次点击button时,都需要从数据库中读取一条记录并在textbox1textbox2中显示。

目前我知道如何从数据库中读取,但如何在每次点击时显示不同的值

Public Class form1
    Dim mysqlconnection As MySqlConnection
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mysqlconnection = New MySqlConnection()
        mysqlconnection.ConnectionString = " Hidden "
        Try
            mysqlconnection.Open()
        Catch myerror As MySqlException
            MessageBox.Show("Cannot connect to database: " & myerror.Message)
        End Try
        Dim myadapter As New MySqlDataAdapter
        Dim sqlquary = "SELECT * FROM bazatestna.Users"
        Dim command As New MySqlCommand
        command.Connection = mysqlconnection
        command.CommandText = sqlquary
        myadapter.SelectCommand = command
        Dim mydata As MySqlDataReader
        mydata = command.ExecuteReader()
        If mydata.HasRows = 0 Then
            MsgBox("Database has no records")
        Else
            'Ths is the part where i need to show values on click
        End If
    End Sub
End Class

我很乐意在加载时从mysql数据库读取所有数据,然后每当我点击按钮显示数据库中的一条记录时。

1 个答案:

答案 0 :(得分:0)

这就是我要做的:我声明一个变量并将其放在Form1 Class:

Public Class Form1
Dim cnt as Integer = 1

然后在按钮点击事件中添加1个cnt增量:

cnt +=1

然后修改您的查询字符串以添加限制:

Dim sqlquary = "SELECT * FROM bazatestna.Users Limit " & cnt & ", 1"

要使程序运行得更快,请将连接声明移动到表单加载事件:

 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    mysqlconnection = New MySqlConnection()
    mysqlconnection.ConnectionString = " Hidden "
    Try
        mysqlconnection.Open()
    Catch myerror As MySqlException
        MessageBox.Show("Cannot connect to database: " & myerror.Message)
    End Try

End Sub