如何更新gridview [英] How to update a gridview

查看:119
本文介绍了如何更新gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表格,表格A有文本框,可以将数据添加到Mysql数据库。



表格B包含一个flexgrid。



当我在flexgrid中编辑一行时,它会打开表单A,但是当我保存时,它会创建另一条记录。我希望它能够更新现有记录。在我的保存按钮上我有



I have 2 forms, Form A has text boxes which adds data to the Mysql database.

Form B includes a flexgrid.

When I edit a row in the flexgrid it opens Form A but when I save, it creates another record. I want it to update existing record. On my save button I have

private void lnkSave_Click(object sender, EventArgs e)
        {
             string constring = "datasource=127.0.0.1;Port=3306;Database=bpos;username=root;password=Pass";
            string Query = "insert into bpos.patchtest (Description,Employee,MemberID,Results,Date) values('" + this.txtDescription.Text + "','" + this.cmbEmployee.Text + "','" + this.txtRefNo2.Text + "','" + Results + "','" + this.metroDateTime1.Text + "') ;";
    
            MySqlConnection conDataBase = new MySqlConnection(constring);
             MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
              MySqlDataReader myReader;
             try
             {
               conDataBase.Open();
              myReader = cmdDataBase.ExecuteReader();
               MessageBox.Show("Saved");
               
               while (myReader.Read())
                {

                 }

              }
             catch (Exception ex)
               {

                   MessageBox.Show(ex.Message);
               }
              }





我不想创建更新按钮。是否可以使用上面的保存按钮更新?



我尝试过:



我尝试使用更新按钮,但我真的不想使用其他按钮。



I don't want to create an Update button. Is it possible to Update using above save button?

What I have tried:

I tried using an update button but I really don't want to use another button.

推荐答案

是 - 但首先不要那样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。



只需发出UPDATE查询而不是INSERT就可以修改行:INSERT总是创建一个新行,UPDATE只会更改现有行。

粗略的语法是:

Yes - but first don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

You can modify rows just by issuing an UPDATE query instead of an INSERT: INSERT always creates a new row, UPDATE only ever changes existing rows.
The rough syntax is:
UPDATE MyTable SET MyColumn = MyNewValue, MyOtherColumn = MyOtherNewValue WHERE MyROwIDColumn=TheIDOfTheColumnToChange





BTW帮自己一个忙:INSERT和UPDATE命令不返回任何行数据 - 因此使用DataReader毫无意义。相反,这些类型的命令应该使用ExecuteNonQuery发出 - 它返回插入或更改的行数。



严重:仔细检查所有代码并摆脱字符串连接 - 这真的很危险!



BTW do yourself a favour: INSERT and UPDATE commands do not return any row data - so using a DataReader is pointless. Instead, these type of commands should be issued with ExecuteNonQuery - which returns the number of rows inserted or changed instead.

And seriously: Go through all your code and get rid of string concatenation - it is really dangerous!


这篇关于如何更新gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆