我的代码错了,但这是我能想到的最接近解决方案的代码。我试图从1个表中为指定用户添加所有点,并将该值插入另一个表中。任何帮助表示赞赏。
这是我的代码:
Imports MySql.Data.MySqlClient
Public Class Leaderboard
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Private Sub submitBtn_Click(sender As Object, e As EventArgs) Handles submitBtn.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;userid=root;password=root99;database=database"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = " select (select SUM(Points) as sumpoints from database.progress where candidate_number_fk = '" & textbox_can.Text & "') (insert into database.leaderboard (username,points) values ('" & textbox_user.Text & "',' sumpoints ' )"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = Command.ExecuteReader
MessageBox.Show("You have successfully submitted your score")
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
My.Forms.Menu1.Show()
Me.Hide()
End Sub
End Class
答案 0 :(得分:1)
您不需要双重查询,因为您可以使用INSERT INTO .. SELECT FROM
构造并在单个查询中完成它,例如
insert into database.leaderboard (username,points)
select 'Niam', SUM(Points) as sumpoints
from database.progress
where candidate_number_fk = '" & textbox_can.Text & "';
请注意,您正在使用字符串连接直接在查询中使用用户输入,这可能会导致 SQL注入。使用参数化查询而不是MySqlParameter
类。