winforms 不确定为什么我的sql更新没有运行,即使看起来没有问题

ibrsph3r  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(135)
private void UpdatePlayerScore(PlayerScore phsc)
{
    string updatescore = 
        "UPDATE `score` SET `PlayerScore`= '" + score +
        "' WHERE `ScoreID`= '" + playersid + "'";
    MySqlConnection conn2 = dbPlayer.GetConnection();
    MySqlCommand cmd2 = new MySqlCommand(updatescore, conn2);
            

    cmd2.ExecuteNonQuery();

    try
    {
                
        MessageBox.Show("New highscore recorded", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (MySqlException ex)
    {
        MessageBox.Show("New highscore not recorded \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    conn2.Close();

}

我不确定为什么我的数据库(Mysql)在phpmyadmin是不更新,即使我收到的新的高分被记录。
我试过使用add with value和add value,但效果似乎不太好

kcrjzv8t

kcrjzv8t1#

好吧,这些都是“琐碎的事情,”首先,try/catch的使用是这样的:
try一个代码,如果代码有错误,catch就会被调用。所以你需要把所有的代码都放进去,如果你想捕捉任何东西的话。令人惊讶的是你可以到达try。
对于try/catch,只需输入以下内容:

private void UpdatePlayerScore(PlayerScore phsc)
{
            

    cmd2.ExecuteNonQuery();

    try
    {
        string updatescore = "UPDATE `score` SET `PlayerScore`= '"+ score +"' WHERE `ScoreID`='" + playersid + "'";
        MySqlConnection conn2 = dbPlayer.GetConnection();
        MySqlCommand cmd2 = new MySqlCommand(updatescore, conn2);

                
        MessageBox.Show("New highscore recorded", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (MySqlException ex)
    {
        MessageBox.Show("New highscore not recorded \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    conn2.Close();

}

其次,您的查询:

string updatescore = "UPDATE `score` SET `PlayerScore`= '"+ score +"' WHERE `ScoreID`='" + playersid + "'";

它看起来可能更像这样:

string updatescore = "UPDATE score SET PlayerScore="+ score +" WHERE ScoreID=" + playersid;

如果PlayerScore是字符串,则:

string updatescore = "UPDATE score SET PlayerScore=\'"+ score +"\' WHERE ScoreID=" + playersid;

实际上,您总是需要在撇号前添加一个反斜杠。

相关问题