【发布时间】:2026-02-06 21:30:01
【问题描述】:
表名:高分
每当我想编辑一个选定的注册(带有 gamer 和 hscore 属性)时,我的 gamer 值会变为“0”,并且高分保持不变。
比如我输入了一个用户:Andrew,高分332,我想编辑成Zack,高分009,变成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;
}
}
【问题讨论】:
-
我假设当你说“它会变成 0 并且高分仍然是 332,从上一个开始。”您正在检查您的 mySQL 表中的结果吗?您是否检查过作为参数传入的值是否正确?
-
你是从主类引用 button1_Click(this,null) 吗?
标签: c# mysql visual-studio insert-update