2015-11-07 73 views
0

下面是一個記分板應用程序的代碼,我試圖維護一個測驗節目的團隊得分。應用程序的工作原理如果你想給A隊打分,先按A鍵,然後按1,2,3,4,5或6鍵來獲得標記(+5,+ 10,+ 15,-5, -10或-15)。 我想爲標記創建一個數據庫並在每次按下按鈕時更新它們。每個按鈕上的數據庫更新按

Label target = new Label(); 
int vA = 0; 

private void frmScoreBoard_KeyUp(object sender, KeyEventArgs e) { 
    if (e.KeyCode == Keys.A) { 
     target = lblScoreA; 
    } 

    if (e.KeyCode == Keys.B) { 
     target = lblScoreB; 
    } 

    if (e.KeyCode == Keys.C) { 
     target = lblScoreC; 
    } 

    if (target.Text != "") { 
     if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1) { 
     vA = int.Parse(target.Text); 
     vA += 5; 

     target.Text = vA.ToString(); 
     } 
     if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2) { 
     vA = int.Parse(target.Text); 
     vA += 10; 
     target.Text = vA.ToString(); 
     } 
     if (e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3) { 
     vA = int.Parse(target.Text); 
     vA += 15; 
     target.Text = vA.ToString(); 
     } 
     if (e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4) { 
     vA = int.Parse(target.Text); 
     vA -= 5; 
     target.Text = vA.ToString(); 
     } 
     if (e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5) { 
     vA = int.Parse(target.Text); 
     vA -= 10; 
     target.Text = vA.ToString(); 
     } 
     if (e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6) { 
     vA = int.Parse(target.Text); 
     vA -= 15; 
     target.Text = vA.ToString(); 
     } 
    } 
} 

我知道ADO.NET和連接,但我不知道怎麼每次做更新。我不需要代碼 - 我只是想知道如何去做。對於ADO.NET

代碼:

using (SqlConnection con = new SqlConnection(CS)) { 
    SqlCommand cmd = new SqlCommand("update tblScore set Score='" + Convert.ToInt32(lblScoreA.Text) + "'where TeamName= '" + Convert.ToInt32(lblTeamA.Text) + "'", con); 
    SqlCommand cmd1 = new SqlCommand("update tblScore set Score='" + Convert.ToInt32(lblScoreB.Text) + "'where TeamName= '" + Convert.ToInt32(lblTeamB.Text) + "'", con); 
    cmd1.ExecuteNonQuery(); 
    SqlCommand cmd2 = new SqlCommand("update tblScore set Score='" + Convert.ToInt32(lblScoreC.Text) + "'where TeamName= '" + Convert.ToInt32(lblTeamC.Text) + "'", con); 
    cmd2.ExecuteNonQuery(); 
} 

誰能幫助?

+0

[SQL注入警報](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該**不**連接在一起你的SQL語句 - 使用**參數化查詢**來代替以避免SQL注入 –

+0

如果我使用參數化查詢,那麼如何將其轉換爲int? –

回答

1

你可以更新了一支具有得分創建一個單獨的方法:

private void UpdateTeamScore(string teamName, int score) { 

    using (SqlConnection con = new SqlConnection(CS)) { 
     con.Open(); 

     using (SqlCommand command = new SqlCommand("UPDATE tblScore SET Score = @Score WHERE TeamName = @TeamName;", con)) { 
     command.Parameters.Add(new SqlParameter("Score", score)); 
     command.Parameters.Add(new SqlParameter("TeamName", teamName)); 
     command.ExecuteNonQuery(); 
     } 
    } 
} 

和獨立的KEYUP邏輯:

private String activeTeam = null; 
private void frmScoreBoard_KeyUp(object sender, KeyEventArgs e) { 

    // If the user used team selection keys 
    if ((e.KeyCode == Keys.A) || (e.KeyCode == Keys.B) || (e.KeyCode == Keys.C)) { 

     // Select the team according to pressed key 
     ActivateTeamForScoring(e); 

     // Return as we don't need to do anything else on this keystroke 
     return; 
    } 

    // If the user came here by pressing the scoring keys 
    else { 

     // If a team wasn't set, return 
     if (activeTeam == null) { return; } 

     // Resolve the score according to pressed key 
     int? score = ResolveScore(e); 

     // If the user pressed correct score key, update 
     if (score != null) { 

     // Perform the score update to database 
     UpdateTeamScore(activeTeam, score.Value); 
     } 

     // Reset active team after scoring 
     activeTeam = null; 
    } 
} 

private void ActivateTeamForScoring(KeyEventArgs e) { 

    // Set the right team to be scored 
    if (e.KeyCode == Keys.A) { 
     activeTeam = lblScoreA; 
    } else if (e.KeyCode == Keys.B) { 
     activeTeam = lblScoreB; 
    } else if (e.KeyCode == Keys.C) { 
     activeTeam = lblScoreC; 
    } 
} 

private int? ResolveScore(KeyEventArgs e) { 

    if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1) { 
    return 5; 
    } else if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2) { 
    return 10; 
    } else if (e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3) { 
    return 15; 
    } else if (e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4) { 
    return -5; 
    } else if (e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5) { 
    return -10; 
    } else if (e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6) { 
    return -15; 

    // If the keystroke was invalid, return null 
    } else { 
    return null; 
    } 
} 
-1

原理工作......你需要打開你的連接之前你開始運行命令就可以了,所以用

con.Open(); 

你可以把所有的UPDATE彙集成一個命令。此外,文本框中的數據是字符串,因此您無需首先將其轉換爲int

using (SqlConnection con = new SqlConnection(CS)) { 
    con.Open(); 
    // Create one string with all the updates in it... 
    string query = string.Format("UPDATE tblScore SET Score={0} WHERE TeamName='{1}'; ", lblScoreA.Text, lblTeamA.Text); 
    query += string.Format("UPDATE tblScore SET Score={0} WHERE TeamName='{1}'; ", lblScoreB.Text, lblTeamB.Text); 
    query += string.Format("UPDATE tblScore SET Score={0} WHERE TeamName='{1}'; ", lblScoreC.Text, lblTeamC.Text); 

    SqlCommand cmd = new SqlCommand(query, con); 
    cmd.ExecuteNonQuery(); 
    }