如何在选中单选按钮以获取计算天数时进行更新

本文关键字:更新 计算 获取 单选按钮 | 更新日期: 2023-09-27 18:06:56

   try
            {
                SqlConnection con = new SqlConnection(Connectionstring);
                con.Open();
                String sql = String.Format("update leavetable set status = '" + status + "' where eid = '" + textBox1.Text + "' and status = 'NULL'");
                SqlCommand cmd = new SqlCommand(sql, con);
                if (cmd.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("status updated");
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                }
                con.Close();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }

在这里,我将leavetable的详细信息显示到文本框中,并将radiobuttons作为批准或取消。

当管理员选择已批准的radiobutton并单击ok时,我想更新该empid的状态为已批准,但empid不是主键。但是当我这样做的时候,它会更新所有的行有emp001的empid例如

如何在选中单选按钮以获取计算天数时进行更新

我很难理解你想说什么…

RadioButton提供Checked事件。如果您订阅它,您可以在每次触发事件时运行更新查询(它也有一个Unchecked事件…)。

当使用String.Format时,试着利用它的优势,用占位符内联地编写一个漂亮的完整字符串,它将在运行时被实际值替换,而不是把它撕下来。

...
myRadioButton.Checked += RunUpdateQuery_OnRadioButtonChecked;
private void RunUpdateQuery_OnRadioButtonChecked(object sender, EventArgs e)
{  
  try
  {    
      using (SqlConnection connection = new SqlConnection(Connectionstring))
      {
          connection.Open();
          String updateQuery = String.Format("UPDATE leavetable SET status = '{0}'  
                                   WHERE eid = '{1}  AND status = 'NULL'",   
                                   status, textBox1.Text);
          SqlCommand commad = new SqlCommand(updateQuerry, connection);
          if (command.ExecuteNonQuery() == 1)
          {
              MessageBox.Show("status updated");
              textBox1.Text = "";
              textBox2.Text = "";
              textBox3.Text = "";
              textBox4.Text = "";
          }
       }
   }
   catch (Exception er)
   {
       MessageBox.Show(er.Message);
   }      
}

由于您希望查询在admin批准后执行,并且该控件由单选按钮设置,请在radiobuttonevent checked上添加代码:

  private void Radiobutton1_Checked(object sender, EventArgs e)
    {  
      try
         {    
           SqlConnection con = new SqlConnection(Connectionstring);
           con.Open(); 
           using (con)
             {          
                String sql = "update leavetable set status =@status where eid =@ID  and status = 'NULL'");
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@status", status);
                cmd.Parameters.AddWithValue("@ID", textBox1.Text);
                if (cmd.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("status updated");
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                }
              }
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }      
    }
编辑:

如果eid不是表中的索引(有多个行具有相同的id),那么显然更新将在多个记录中完成。这个事实与您的查询设置有关,而与编程无关。

这是由你来决定你想要选择什么条件来正确更新你的表的记录。

很可能你需要添加一个额外的where子句,如果我正确理解你有一个时间戳字段nooofdays,你也可以使用这个:

update leavetable set status =@status where eid =@ID and nooofdays=@param1 and status = 'NULL'");
cmd.Parameters.AddWithValue("@param1", textBox2.Text);

希望能有所帮助,因为很难理解你…