mysql - 将从 mysql 查询获得的值显示到 VB.net 文本框中

标签 mysql vb.net count textbox

我试图在 vb.net 的文本框中显示我从 COUNT(*) 查询中获得的值,但不是显示获得的值,而是显示实际查询。

这是我的代码:

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

这是我的程序的屏幕截图:link

当尝试修复代码时,我将其更改为将 takenout.Text = (query) 替换为 takenout.Text = (reader.read()) 并删除上面的 reader.read()) 和 dadapter.SelectCommand = command

这里是修改后的代码:

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。

这是一张图片:link

最佳答案

改用 command.ExecuteScalar()

关于mysql - 将从 mysql 查询获得的值显示到 VB.net 文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161371/

相关文章:

php - 为什么这是 PHP 中的错误 MYSQL 查询?

vb.net - 如何将 MDI 子窗体放置在 MDIParent 的右下角?

Excel - 检查列表中两列的值是否为新值并向它们传递新 ID

java - 在java中更改整数的值

python - 计算可迭代对象中元素的数量

mysql - 尽管在 MySQL 4.1.25 中有效,但 MySQL 5.5 中的混合连接查询存在问题

MySQL返回逆数?搜索结果或不包含的结果

c# - 远程MySQL访问耗时较长

javascript - 使用 JSON.stringify 将 jQuery AJAX 发布到我的 ASP.NET web 服务

c# - EF4 : LINQ 2 Entities query works in C# but not in VB