我想顯示一個值,我obatain從一個文本框中的COUNT(*)查詢在VB.NET中,而不是顯示獲取的值,它顯示實際查詢。顯示從一個MySQL查詢獲得的值到一個VB.net文本框
這裏是我的代碼:
Imports MySql.Data.MySqlClient
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
dadapter.SelectCommand = command
If reader.HasRows Then
reader.Read()
takenout.Text = (query)
End If
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
當試圖修復我改變它與takenout取代takenout.Text =(查詢)的代碼。文本=(reader.read())和刪除reader.read())的上方和dadapter.SelectCommand =命令
這裏是改變的代碼:
Imports MySql.Data.MySqlClient
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
If reader.HasRows() Then
takenout.Text = (reader.Read())
End If
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
此代碼無法解決問題,而是在按下按鈕時在文本框中顯示true,無論用戶名文本框中的內容如何。
而不是一味地試圖隨機碼,你應該花10分鐘在MSDN怎麼看[DBDataReader(https://msdn.microsoft.com/en-us/library/system.data.common。 dbdatareader(v = vs.110).aspx)的作品。一旦你讀取()一行,你就需要獲取數據。但對於只能返回一行的'Count()'查詢,您不需要讀取器,'command.ExecuteScalar()'將返回值 – Plutonix