Hello, i am trying to make a very very simplistic game in visual basic with using mysql kind of as the server to hold all account information such as name, coins, etc.
Can you please check why I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''6` WHERE `id` = 5' at line 1
Ok so, I have:
Form1.vb
MainGame.vb
Forms and when opened the progam, Form1 will be the first to be opened.
Form1:
MainGame:
Pictures:
Attachment 95357Attachment 95359
Can you please check why I am getting this error:
Quote:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''6` WHERE `id` = 5' at line 1
Form1.vb
MainGame.vb
Forms and when opened the progam, Form1 will be the first to be opened.
Form1:
Code:
Imports MySql.Data.MySqlClient
Public Class Form1
Private mysql_host = "localhost"
Private mysql_user = "root"
Private mysql_pass = ""
Private mysql_db = "simplegame"
Public SQLConnect As String = "Server= " + mysql_host + ";" + "User ID=" + mysql_user + ";" + "Password=" + mysql_pass + ";" + "Database=" + mysql_db
Private SQLConnection As New MySqlConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SQLStatement As String = "INSERT INTO users(name, coins) VALUES('" & TextBox1.Text & "',0)"
Dim cmd As New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
MsgBox("Successfully Registered " + TextBox1.Text + "!")
MainGame.Show()
MainGame.Text = TextBox1.Text
Me.Hide()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SQLConnection.ConnectionString = SQLConnect
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
MsgBox("Successfully connect to Server.", MsgBoxStyle.Information)
End If
Catch ex As Exception
MsgBox("This Server is currently offline.", MsgBoxStyle.Critical)
Me.Close()
End Try
End Sub
End Class
MainGame:
Code:
Imports MySql.Data.MySqlClient
Public Class MainGame
Private coins As Integer = 0
Private Sub RerfreshCoins()
Label1.Text = "Coins: " + coins.ToString()
End Sub
Private Sub MainGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "Coins: " + coins
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
coins += 1
RerfreshCoins()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Using con As New MySqlConnection(Form1.SQLConnect)
con.Open()
Using cmd As MySqlCommand = con.CreateCommand
Dim AccountName As String = Me.Text
Dim Accountid As Integer
cmd.CommandText = "SELECT `id`,`name` FROM `users` WHERE `name` = '" & AccountName & "'"
Using reader As MySqlDataReader = cmd.ExecuteReader
If Not reader.Read Then
Return
End If
Accountid = reader.GetInt32(0)
reader.Close()
Using update As MySqlCommand = con.CreateCommand
update.CommandText = "UPDATE `users` SET `coins` = '" & coins & "` WHERE `id` = " & Accountid
update.ExecuteNonQuery()
End Using
End Using
End Using
End Using
End Sub
End Class
Pictures:
Attachment 95357Attachment 95359