如何仅更新选定的行?目前下面的代码更新整行甚至选择单行也。 [英] How to Update selected row only? Currently below code updating whole rows even selected single row also.

查看:51
本文介绍了如何仅更新选定的行?目前下面的代码更新整行甚至选择单行也。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void subtkchgbtn_Click(object sender, EventArgs e)
        {
            string Emp_No_TakeCharge = emptclbl.Text;
            string Solution = TextBox5.Text;
            string Date_Time_TakeCharge = da_titxt2.Text;

            using (MySqlConnection con = new MySqlConnection(constr))
            {
                MySqlCommand command = new MySqlCommand("UPDATE wsdtrackin set Emp_No_TakeCharge=@Emp_No_TakeCharge, Solution=@Solution, Date_Time_TakeCharge=@Date_Time_TakeCharge", con);
                
                        command.Parameters.AddWithValue("@Emp_No_TakeCharge", emptclbl.Text);
                        command.Parameters.AddWithValue("@Solution", TextBox5.Text);
                        command.Parameters.AddWithValue("@Date_Time_TakeCharge", da_titxt2.Text);
                        command.Connection = con;
                        con.Open();     
                        command.ExecuteNonQuery();
                        con.Close();                    
            }
            if (IsPostBack)
            {
                TextBox5.Text = "";
                da_titxt2.Text = "";
            }
            using (MySqlConnection con = new MySqlConnection(constr))
            {
                using (MySqlCommand cmd = new MySqlCommand("SELECT ID, Emp_No_Up, Pass_Over_To, Emp_No_TakeCharge, Prober_No, Root_Cause, Solution, Date_Time_Up, Date_Time_TakeCharge FROM wsdtrackin"))
                {
                    using (MySqlDataAdapter sda = new MySqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;

                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            GridViewpssover.DataSource = dt;
                            GridViewpssover.DataBind();
                        }
                    }
                }
            }
        }

        protected void GridViewpssover_SelectedIndexChanged(object sender, EventArgs e)
        {
            emptclbl.Text = GridViewpssover.SelectedRow.Cells[4].Text;
            TextBox5.Text = GridViewpssover.SelectedRow.Cells[7].Text;
            da_titxt2.Text = GridViewpssover.SelectedRow.Cells[9].Text;           
        }     
    }
 }





推荐答案

Yea Mathi是对的。你缺少where子句。因此,它将更新表中的所有行。



如果添加where子句并在列上添加条件(建议列是ID,它应该是唯一的或者是primark键),然后只更新一个匹配的行。
Yea Mathi is correct. You are missing where clause. So, it will update all rows in your table.

If you add a where clause and put a condition on a column (recommended column is ID, which should be unique or primark key), then that will update only a single matched row.


这篇关于如何仅更新选定的行?目前下面的代码更新整行甚至选择单行也。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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