这就是我的表格 –
image.
当表单加载时,我想检索其用户名在Label1中的播放器数据,因此我可以在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注入,使用正确的对象处理,Try-Catch块来正确处理异常.
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