C#Winform MySql更新按钮错误(它将更新所有记录)
更新:问题已解决;亲爱的所有人在你们的帮助下,我刚刚意识到了这个问题,首先我将ID列从db添加到datagridview,同时我还将文本框添加到我的FormDesign中。在那之后,我只是把“where”和“id”条件放在下面。谢谢大家C#Winform MySql更新按钮错误(它将更新所有记录),c#,mysql,winforms,button,sql-update,C#,Mysql,Winforms,Button,Sql Update,更新:问题已解决;亲爱的所有人在你们的帮助下,我刚刚意识到了这个问题,首先我将ID列从db添加到datagridview,同时我还将文本框添加到我的FormDesign中。在那之后,我只是把“where”和“id”条件放在下面。谢谢大家 string Query = "update doguAkdenizApp.team set id='" + this.txtEkipKayitNo.Text + "', name='" + this.txtEkipIsim.Text + "', surname
string Query = "update doguAkdenizApp.team set id='" + this.txtEkipKayitNo.Text + "', name='" + this.txtEkipIsim.Text + "', surname='" + this.txtEkipSoyisim.Text + "', birth='" + this.dtpEkipDogum.Text + "', telephone='" + this.txtEkipTelefon.Text + "', email='" + this.txtEkipEposta.Text + "', city='" + this.cbEkipSehir.Text + "', adress='" + this.txtEkipAdres.Text + "', recorddate='"+this.dtpEkipDogum.Text+ "' where id='" + this.txtEkipKayitNo.Text + "';";
我对下面的代码块有问题,我使用“更新”按钮编辑所选行的数据,但不幸的是,它更新了插入数据库的所有记录
如何仅为所选行延迟代码
private void btnEkipGuncelle_Click(object sender, EventArgs e)
{ string myConnection = "datasource=root;port=root;username=root;password=root";
string Query = "update doguAkdenizApp.team set name='" + this.txtEkipIsim.Text + "', surname='" + this.txtEkipSoyisim.Text + "', birth='" + this.dtpEkipDogum.Text + "', telephone='" + this.txtEkipTelefon.Text + "', email='" + this.txtEkipEposta.Text + "', city='" + this.cbEkipSehir.Text + "', adress='" + this.txtEkipAdres.Text + "', recorddate='"+this.dtpEkipDogum.Text+"';";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
MySqlDataReader myReader;
try
{
myConn.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Güncelleme başarılı!"); //Update success notification
while (myReader.Read()) { }
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
loadTable();
clearAllTxt();*/
}
非常感谢,努里
update doguAkdenizApp.team set name='' where some condition
你忘了哪里的条件了。
现在,您编写代码来更新表中的整行,如果您将添加一个where条件,如
where id=3
,那么只有id为的行(仅举例)将被更新这纯粹与SQL有关,您调用此SQL查询:
string Query = "update doguAkdenizApp.team set name='" + this.txtEkipIsim.Text + "', surname='" + this.txtEkipSoyisim.Text + "', birth='" + this.dtpEkipDogum.Text + "', telephone='" + this.txtEkipTelefon.Text + "', email='" + this.txtEkipEposta.Text + "', city='" + this.cbEkipSehir.Text + "', adress='" + this.txtEkipAdres.Text + "', recorddate='"+this.dtpEkipDogum.Text+"';";
SQL将首先选择要更新的所有行,然后进行更新,最后将所有更改提交到DB。在您的示例中它做了什么?它将选择所有条目(因为缺少条件),然后更新所有条目
它应该是这样的:
string Q = "UPDATE doguAkdenizApp.team set /* all the set*/ WHERE surname='"+this.txtEkipSoyisim.Text+"'";
这不是构建SQL查询的正确方法。这是一个非常有问题的问题,执行DB Ops并没有那么繁琐。亲爱的@puroponix,还有更好的查询吗?仅为datagrid上的选定行更新?那么我如何让应用程序了解用户选择的记录?亲爱的Tatran,如果我只使用一列,它将仅更新选定行?我做得对吗?它将根据条件(放在
WHERE
子句中)更新所有行。您可以更新任何列,但您将指定的所有值都将更新。其他的(不会被输入)将被忽略。