首页 > 代码库 > 个人作业
个人作业
计划
估计此程序需要3-5天。
开发
需求分析
用户故事:作为一个观众,我希望了解某一场比赛的比分,以便了解赛况。(满意条件:精确到每一局的结果比分)。
代码规范:使用Vs2010和帕斯卡命名法和骆驼命名法 。
具体代码
查询及调用方法代码:
private void cmbLoad()
{
cmbName.Items.Clear();
string sql = "select game from paiqiu";
SqlDataReader reader = SqlHelper.ExecuteReader(sql);
if (reader.HasRows)
{
while (reader.Read())
{
cmbName.Items.Add(reader[0]);
}
}
reader.Close();
}
private void dgvLoad()
{
string sql = "select * from paiqiu";
dgvGame.DataSource = SqlHelper.ExecuteDataTable(sql);
}
private void Form2_Load(object sender, EventArgs e)
{
cmbLoad();
dgvLoad();
}
private void select_Click(object sender, EventArgs e)//单个查询
{
StringBuilder sql = new StringBuilder("select * from paiqiu where 1=1");
if (!string.IsNullOrEmpty(cmbName.Text))
{
sql.Append(" and game like ‘%" + cmbName.Text + "%‘");
}
SqlDataReader reader= SqlHelper.ExecuteReader(sql.ToString());
if (reader.HasRows)
{
dgvGame.DataSource = SqlHelper.ExecuteDataTable(sql.ToString());
}
else
{
MessageBox.Show("没有查询到比赛记录!");
}
}
private void lblWrite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)//跳转计分页面
{
this.Close();
writeHistory0 a = new writeHistory0();
a.Show();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Application.Exit();
}
private void dgvGame_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int x = e.ColumnIndex;
int y = e.RowIndex;
if(x==0&&y!=-1)
{
child ch = new child();
ch.xq(dgvGame.CurrentRow.Cells[11].Value.ToString());
ch.Show();
}
}连接数据库代码:
private static readonly string strConn = ConfigurationManager.ConnectionStrings["paiqiulianjie"].ConnectionString;
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, strConn))
{
if (pms != null)
{
adapter.SelectCommand.Parameters.AddRange(pms);
}
adapter.Fill(dt);
}
return dt;
}
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
{
SqlConnection con = new SqlConnection(strConn);
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
try
{
con.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch (Exception)
{
con.Close();
con.Dispose();
throw;
}
}
}
public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(strConn))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
个人作业