作业:作为一个观众,我希望了解某一场比赛的比分,以便了解赛况。(满意条件:精确到每一局的结果比分)
排球计分程序之--用户故事。
根据我当前的能力和老师要求,我想的页面效果大概是这样:
数据库的表:
运行效果如下:
选择第三局:
已胜局数还没写出来,比赛最终结果还没写出来;
部分代码如下:
public class PartResult { public int partNum { get; set; } public int resultA { get; set; } public int resultB { get; set; } }
private void Form1_Load(object sender, EventArgs e) { //设置默认选择显示“第五局” cmbSelectPart.SelectedIndex = 4; } private void cmbSelectPart_SelectedIndexChanged(object sender, EventArgs e) { Display(cmbSelectPart.SelectedIndex + 1); } //连接字符串 public static string strCon = ConfigurationManager.ConnectionStrings["connectionStr_myPC"].ConnectionString; //定义获得数据库中的每局最终比分的数据的方法: public List<PartResult> GetPartResult(int p=5) { SqlConnection con = new SqlConnection(strCon); List<PartResult> list = null; try { //打开连接 con.Open(); //用于查询前p条记录,每条用一个ThePart对象tp保存,放在list里面 string sql = "select top(" + p + ") * from PartResult"; SqlCommand cmd = new SqlCommand(sql, con); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { list = new List<PartResult>(); while (reader.Read()) { PartResult pr = new PartResult(); pr.resultA = reader.GetInt32(1); pr.resultB = reader.GetInt32(2); list.Add(pr); //这里每次都添加名为pr的变量,却可以,why? } } } catch (Exception ex) { txtA1.Text=ex.ToString(); } con.Close(); return list; } //定义把获取的数据显示到控件中的方法: public void Display(int p ) { List<PartResult> list = new List<PartResult>(); list = GetPartResult(p); //给当局比分填值 txtAScore.Text = list[p-1].resultA.ToString(); txtBScore.Text = list[p-1].resultB.ToString(); //对象集合将值显示到控件中 if (p == 1) { txtA1.Text = list[0].resultA.ToString(); txtB1.Text = list[0].resultB.ToString(); txtA2.Text = ""; txtB2.Text = ""; txtA3.Text = ""; txtB3.Text = ""; txtA4.Text = ""; txtB4.Text = ""; txtA5.Text = ""; txtB5.Text = ""; } if (p == 2) { txtA1.Text = list[0].resultA.ToString(); txtB1.Text = list[0].resultB.ToString(); txtA2.Text = list[1].resultA.ToString(); txtB2.Text = list[1].resultB.ToString(); txtA3.Text = ""; txtB3.Text = "";
时间: 2024-10-24 21:24:49