C#Winform MySql Update Button错误(它更新了所有记录)

时间:2017-02-05 18:56:27

标签: c# mysql winforms button sql-update

更新:问题解决了;亲爱的所有你的帮助我刚刚意识到问题,首先我从db添加ID列到datagridview我也添加了一个文本框到我的formdesing。之后,我只是将“where”和“id”条件设置如下。谢谢大家!

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();*/
    }

非常感谢,Nuri。

2 个答案:

答案 0 :(得分:0)

def fileToList(url=''):
    source = urllib2.urlopen(url)

    return [', '.join(l.split('$')[::-1]) for l in source.split('\n') if l.strip()]

print(fileToList())

你忘记了哪里的条件。 现在你写了更新表中的整行,如果你要添加像['2.4, 14km WSW of Willow, Alaska', '0.9, 4km NNW of The Geysers, California', '2.1, 13km ESE of Coalinga, California'] 这样的where条件,那么只有id(仅举例)的行才会更新

答案 1 :(得分:0)

这与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+"'";