每次按下按钮都会更新数据库
Database update on each button press
下面是记分牌应用程序的代码,我试图在其中维护智力竞赛节目的团队分数。该应用程序的工作原理是,如果您想给 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();
}
有人能帮忙吗?
您可以创建一个单独的方法来更新带有分数的团队:
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;
}
}
原则有效...您需要先打开连接,然后再对其执行 运行 命令,因此请使用
con.Open();
您可以将所有更新放在一个命令中。另外,文本框中的数据是字符串,因此您无需先将其转换为 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();
}
下面是记分牌应用程序的代码,我试图在其中维护智力竞赛节目的团队分数。该应用程序的工作原理是,如果您想给 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();
}
有人能帮忙吗?
您可以创建一个单独的方法来更新带有分数的团队:
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;
}
}
原则有效...您需要先打开连接,然后再对其执行 运行 命令,因此请使用
con.Open();
您可以将所有更新放在一个命令中。另外,文本框中的数据是字符串,因此您无需先将其转换为 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();
}