c#
2014-06-06 49 views -2 likes 
-2
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不是主鍵。但是當我這樣做,它會更新所有具有EMPID作爲emp001例如行

+0

我不清楚什麼不是在你的代碼中工作? – HABJAN

+0

這裏來的SQLi評論... –

+0

是不是一個小時前問的幾乎相同的問題? http://stackoverflow.com/questions/24075036/update-command-is-not-working –

回答

0

既然你想通過管理員aproval執行的查詢,並且這種控制是通過一個單選按鈕設置增加您的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 [email protected] where eid [email protected] 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 [email protected] where eid [email protected] and [email protected] and status = 'NULL'"); 
cmd.Parameters.AddWithValue("@param1", textBox2.Text); 

希望它能幫助,因爲它相當diffult理解你...

+0

您正在過濾空值和eId,其中此ID是一個共享值。你需要有一個不共享的領域,並將其納入你的WHERE條件... – BionicCode

+0

那麼該怎麼辦,我應該不得不改變表結構 – user3682545

+0

狀態字段將是空的,直到管理員檢查一旦他覈查爲批准它將被填充爲批准或取消,所以我採取了NULL – user3682545

1

這是我很難明白你想說什麼......

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); 
    }  
} 
相關問題