gpt4 book ai didi

mysql - VB - MySql 从数据库中选择

转载 作者:可可西里 更新时间:2023-11-01 07:35:21 27 4
gpt4 key购买 nike

这是我的表单的样子 - image .

当表单加载时,我想检索 usernameLabel1 中的播放器数据,这样我就可以在中显示它的 points Label2

到目前为止,这是我的代码:

Dim conn As MySqlConnection
conn = New MySqlConnection("server=REMOVED;Port=REMOVED; user id=REMOVED; password=REMOVED; database=REMOVED")
Dim username As Boolean = True
conn.Open()
Dim sqlquery As String = "SELECT Name FROM NewTable WERE Name='" & My.Settings.Name & "';"
Dim data As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = sqlquery
command.Connection = conn
adapter.SelectCommand = command
data = command.ExecuteReader
While data.Read()
Label1.Text = data(1).ToString
Label2.Text = data(3).ToString
End While

data.Close()
conn.Close()

如有任何帮助,我们将不胜感激。

最佳答案

您应该参数化您的查询以避免 sql 注入(inject),Using 以正确处理对象,Try-Catch block 以正确处理异常。

Dim connString As String = "server=REMOVED;Port=REMOVED; user id=REMOVED; password=REMOVED; database=REMOVED"
Dim sqlQuery As String = "SELECT Name, Points FROM NewTable WHERE Name = @uname"
Using sqlConn As New MySqlConnection(connString)
Using sqlComm As New MySqlCommand()
With sqlComm
.Connection = sqlConn
.Commandtext = sqlQuery
.CommandType = CommandType.Text
.Parameters.AddWithValue("@uname", My.Settings.Name)
End With
Try
sqlConn.Open()
Dim sqlReader As MySqlDataReader = sqlComm.ExecuteReader()
While sqlReader.Read()
Label1.Text = sqlReader("Name").ToString()
Label2.Text = sqlReader("Points").ToString()
End While
Catch ex As MySQLException
' add your exception here '
End Try
End Using
End Using

关于mysql - VB - MySql 从数据库中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18801038/

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