VB - MySql select from database

This is what my shape looks like - an image .

When the forms load, I would like to get the data of the player whose username is in Label1 , so I could display his points in Label2 .

Here is my code:

 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() 

Any help would be greatly appreciated.

+4
source share
1 answer

You should parameterize your query to avoid sql injection, Using to properly remove objects, Try-Catch to properly handle the exception.

 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 
+2
source

Source: https://habr.com/ru/post/1502173/


All Articles