2015-11-07 69 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(); 
    }