Vb.net Visual basic-增加分数

Vb.net Visual basic-增加分数,vb.net,Vb.net,我终于找到了要显示的消息框!但是现在我的代码在数据库中没有增加,有人能帮我吗 提前感谢在修复您的输入错误(登录文本框后面的空格和检索到的字段名称)后,您仍然无法执行更新数据库的sql文本 如果WHERE条件找不到任何要更新的内容,则可以简化代码,从而理解更新查询没有任何效果。此外,在尝试执行MySqlCommand时保持MySqlDataReader打开将触发MySql NET connector中的错误。(无法使用datareader正在使用的连接)。我们可以尝试在对ExecuteReader

我终于找到了要显示的消息框!但是现在我的代码在数据库中没有增加,有人能帮我吗

提前感谢

在修复您的输入错误(登录文本框后面的空格和检索到的字段名称)后,您仍然无法执行更新数据库的sql文本

如果WHERE条件找不到任何要更新的内容,则可以简化代码,从而理解更新查询没有任何效果。此外,在尝试执行MySqlCommand时保持MySqlDataReader打开将触发MySql NET connector中的错误。(无法使用datareader正在使用的连接)。我们可以尝试在对ExecuteReader的一次调用中执行这两条语句,使用分号分隔每个命令,当然,还可以使用参数而不是字符串连接

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim READER As MySqlDataReader
    Dim Query As String
    Dim connection As MySqlConnection
    Dim COMMAND As MySqlCommand
    Dim item As Object
    Try
        item = InputBox("What is the item?", "InputBox Test", "Type the item here.")
        If item = "shoe" Then
            Dim connStr As String = ""
            Dim connection As New MySqlConnection(connStr)
            connection.Open()
            Query = "select * from table where username= '" & Login.txtusername.Text & " '"
            COMMAND = New MySqlCommand(Query, connection)
            READER = COMMAND.ExecuteReader
            If (READER.Read() = True) Then
                Query = "UPDATE table set noOfItems = noOfItems+1, week1 = 'found' where username= '" & Login.txtusername.Text & "'"
                Dim noOfItems As Integer
                Dim username As String
                noOfItems = READER("noOfItems") + 1
                username = READER("username")
                MessageBox.Show(username & "- The number of items you now have is: " & noOfGeocaches)
            End If
        Else
            MsgBox("Unlucky, Incorrect item. Please see hints. Your score still remains the same")

        End If
    Catch ex As Exception
        MessageBox.Show("Error")
    End Try

您的select查询中有输入错误。您正在用户名值后添加一个空格。顺便说一句,如果您希望sql字符串对您的表产生任何影响,那么它应该由MySqlCommand执行。这导致了另一个问题。datareader打开时无法使用命令。另外,为什么要增加名为noOfItems的字段,然后尝试显示noOfGeocaches的值?您好@Steve我感谢您的帮助!我已经对查询输入进行了排序!并将变量更改为noOfItems(认为这是一个输入错误)。但是,数据库中仍然没有增量。我将进一步研究mysqlcommand,如果您有任何想法,关于如何解决这个问题,请随意更正我的代码:)@Steve如果我切换这两个查询,我注意到数据库会增加值,但是消息框看起来并不友好!我真的很感激这一点,它就像一个魅力,我会花时间理解这段代码:)而且我用dim替换了所有的using,仍然工作得很好。。我在使用时遇到了一个错误function@mr-高效地格式化你的代码?也许它会编译。
' Prepare the string for both commands to execute
Query = "UPDATE table set noOfItems = noOfItems+1, " & _ 
        "week1 = 'found' where username= @name; " & _
        "SELECT noOfItems FROM table WHERE username = @name"

' You already know the username, don't you?
Dim username = Login.txtusername.Text

' Create the connection and the command inside a using block to
' facilitate closing and disposing of these objects.. exceptions included
Using connection = New MySqlConnection(connStr)
Using COMMAND = New MySqlCommand(Query, connection)
    connection.Open()

    ' Set the parameter value required by both commands.
    COMMAND.Parameters.Add("@name", MySqlDbType.VarChar).Value = username

    ' Again create the reader in a using block
    Using READER = COMMAND.ExecuteReader
        If READER.Read() Then
            Dim noOfItems As Integer
            noOfItems = READER("noOfItems")
            MessageBox.Show(username & "- The number of items you now have is: " & noOfItems )
        End If
   End Using
End Using
End Using