如何将查询结果转换为变量并将其显示在标签中。我曾尝试使用DR.Read
和While DR.Read
,但仍然失败。
这是我的代码示例
Public dataCNN As New MySqlConnection
Public dataCMD As New MySqlCommand
Public DR As MySqlDataReader
Public DA As MySqlDataAdapter
Public DT As DataTable
Public QUERY As String
Public Sub dbConnection()
dataCNN = New MySqlConnection
dataCNN.ConnectionString = "server=localhost;userid=root;password=password;database=data"
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call dbConnection()
txt1.Focus()
dataCNN.Open()
QUERY = "SELECT * FROM data.numbering WHERE id=1"
dataCMD = New MySqlCommand(QUERY, dataCNN)
DR = dataCMD.ExecuteReader()
If DR.HasRows Then
While DR.Read
Label1.Text = DR("number").ToString()
End While
End If
dataCNN.Close()
End Sub
Private Sub txt1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt1.KeyPress
Dim a, b, c As Integer
If (Val(Asc(e.KeyChar) = 33)) Then
Label2.Text = "Teller 1 is now serving number"
a = Label1.Text + 1
Label1.Text = a
DA = New MySqlDataAdapter("UPDATE data.numbering SET number='" & a & "' WHERE id=1", dataCNN)
End If
'MsgBox("" & Val(Asc(e.KeyChar)))
End Sub
答案 0 :(得分:0)
你应该添加,
DR = dataCMD.ExecuteReader()
后
dataCMD = New MySqlCommand(QUERY, dataCNN)
编辑:如您对更新的评论中所述,您可以这样做
Using connection As New MySqlConnection(connectionString)
Dim Mycmd As New MySqlCommand(yourquery, connection)
Mycmd.Connection.Open()
Mycmd.ExecuteNonQuery()
End Using
如果您使用'使用',则无需关闭连接。