gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:22:01 25 4
gpt4 key购买 nike

我试图在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com