以下是记分牌应用程序的代码,其中我试图维持智力竞赛节目的团队分数。该应用程序的工作原理如果你想给团队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 :(得分: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 :(得分:-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();
}