数据库没有更新,它只是重定向而不是崩溃 这是代码:
protected void cmdUpdate_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("server=localhost;uid=root;" + "pwd=password;database=ddt_data");
conn.Open();
string command = String.Format("update `fixture` set `referee`='" + this.txtRef.Text + "',`ScoreA`='" + this.txtScoreA.Text + "',`ScoreB`='" + this.txtScoreB.Text + "',`Winner`='" + this.txtWinner.Text + "' where `idfixture`='" + this.txtFixtureID.Text + "';", conn);
//MySqlDataReader Reader;
MySqlCommand Update = new MySqlCommand(command, conn);
Update.Connection = conn;
//Update.Parameters.AddWithValue("@Date", txtDate.Text);
//Update.Parameters.AddWithValue("@Time", txtTime.Text);
//Update.Parameters.AddWithValue("@Team A", txtTeamA.Text);
//Update.Parameters.AddWithValue("@Team B", txtTeamB.Text);
//Update.Parameters.AddWithValue("@Referee", txtRef.Text);
//Update.Parameters.AddWithValue("@ScoreA", txtScoreA.Text);
//Update.Parameters.AddWithValue("@ScoreB", txtScoreB.Text);
//Update.Parameters.AddWithValue("@Winner", txtWinner.Text);
//Reader = Update.ExecuteReader();
//while (Reader.Read())
//{
//}
Update.ExecuteNonQuery();
conn.Close();
Response.Redirect("FixtureEdit.aspx");
}
答案 0 :(得分:0)
我修复了CommandText值并使用了参数化的SQL语句。确保参数名称与下面的列表匹配。
protected void cmdUpdate_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("server=localhost;uid=root;" +
"pwd=password;database=ddt_data");
conn.Open();
string command = "UPDATE fixture SET referee=@referee, scorea=@scorea, " +
"scoreb=@scoreb, winner=@winner WHERE idfixture=@idfixture";
MySqlCommmand update = new MySqlCommand(command, conn);
update.Parameters.AddWithValue("@referee", this.txtRef.Text);
update.Parameters.AddWithValue("@scorea", this.txtScoreA.Text);
update.Parameters.AddWithValue("@scoreb", this.txtScoreB.Text);
update.Parameters.AddWithValue("@winner", this.txtWinner.Text);
update.Parameters.AddWithValue("idfixture", this.txtFixtureID.Text);
// not sure what you want to achieve from this
// MySqlDataReader reader = update.ExecuteReader();
// while (reader.Read())
// {
// do something here?
// }
update.ExecuteNonQuery(); // use this if you don't need the DataReader
conn.Close();
conn.Dispose();
Response.Redirect("FixtureEdit.aspx");
}