将列表框中的数据编辑到我的SQL数据库中

问题描述 投票:0回答:1

表格名称:高分

[每当我要编辑选定的注册(具有游戏玩家和hscore属性)时,我的游戏玩家的值都会变成“ 0”,而高得分则保持不变。

例如,我输入了一个用户:Andrew具有332个高分,我想编辑为009个高分的Zack,变成0并且高分仍然是332(先前的高分注册)

程序的主要任务是从列表框向MySQL数据库插入,编辑和删除数据。

这是我的原始方法:

public static void edit(Highscore hs)
    {
        MySqlConnection con = DBConnection.getConnection();

        if (con == null)
        {
            throw new Exception("Conexiunea la baza de date nu s-a realizat.");
        }

        MySqlCommand cmd = con.CreateCommand();

        cmd.CommandText = "UPDATE highscores SET gamer=@gamer,highscore=@hscore WHERE id=@id ";
        cmd.Parameters.AddWithValue("@gamer", hs.Gamer);
        cmd.Parameters.AddWithValue("@hscore", hs.Hscore);
        cmd.Parameters.AddWithValue("@id", hs.Id);



        if (cmd.ExecuteNonQuery() != 1)
        {
            throw new Exception("Editarea nu s-a putut face.");
        }

        con.Close();

    }

而且我想将该方法调用到Main类中:

 private void edit_Click(object sender, EventArgs e)
    {
        DialogResult resDiag = MessageBox.Show("Sunteti sigur ca doriti sa editati aceasta inregistrare?", "EDITEAZA", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);



        if (resDiag == DialogResult.Yes)
        {

            Highscore hs = listBox1.SelectedItem as Highscore;
            HighscoresDAO.edit(hs);


            button1_Click(this, null);



        }
        else
        {
            return;
        }

    }
c# mysql visual-studio insert-update
1个回答
0
投票

问题解决了!我忘记提供适当的参数。

© www.soinside.com 2019 - 2024. All rights reserved.