如何通过visual basic执行SUM查询?

时间:2016-12-17 15:32:16

标签: mysql vb.net

我试图将某个用户从mysql数据库表中获得的积分加起来

我的代码返回错误,说明语法不正确,我无法弄清楚如何使其工作>

这是我的代码:

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 SUM(Points) * from database.progress where candidate_number_fk = '" & textbox_can.Text & "'"
        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

1 个答案:

答案 0 :(得分:0)

这是不正确的SQL语法:

select SUM(Points) *
from database.progress
where . . .

据推测,你打算:

select SUM(Points) as sumpoints
from database.progress
where . . .

然后,您可以按名称从结果集中提取字段。

注意:您的代码可能存在其他问题。这只解决SQL查询。