十八周作业
·计划
估计这个任务需要7天
·开发
*需求分析
用户故事:作为一个排球教练,我希望知道每个队员的技术得分情况,以便后期对每个队员的训练计划进行调整。
*生成设计文档
任务:教练通过选择想要查询的队员名字来查询本队某队员的技术得分。
*设计复审:
将编写的程序进行生成,进行设计复审。看看是否生成错误,如果错误进行修改。
*代码规范:
利用VS对该程序进行代码规范。
*具体设计:
具体编码:
Web.config:
<configuration>
<connectionStrings>
<add name="itcast" connectionString="server=.;initial catalog=itcast;integrated security=true;"/>
</connectionStrings>
</configuration>
public class Dal
{
public List<Model> GetAll()
{
string sql = "select * from Table_1";
List<Model> stu = new List<Model>();
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql))
{
if (reader.HasRows)
{
while (reader.Read())
{
Model Mod = new Model();
Mod.ID = (int)reader["ID"];
Mod.Name = reader["Name"].ToString();
Mod.One = reader["One"].ToString();
Mod.Two = reader["Two"].ToString();
Mod.Three = reader["Three"].ToString();
Mod.Four = Convert.IsDBNull(reader["Four"]) ? null : reader["Four"].ToString();
Mod.Five = Convert.IsDBNull(reader["Five"]) ? null : reader["Five"].ToString();
stu.Add(Mod);
}
}
}
return stu;
}
public List<Model> GetAll1()
{
string sql = "select * from Table_4";
List<Model> stu = new List<Model>();
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql))
{
if (reader.HasRows)
{
while (reader.Read())
{
Model Mod = new Model();
Mod.Name = reader["Name"].ToString();
Mod.Fq = reader["Fq"].ToString();
Mod.Lw = reader["Lw"].ToString();
Mod.Zg = reader["Zg"].ToString();
Mod.Yg = reader["Yg"].ToString();
Mod.Cs = Convert.IsDBNull(reader["Cs"]) ? null : reader["Cs"].ToString();
Mod.Fs = Convert.IsDBNull(reader["Fs"]) ? null : reader["Fs"].ToString();
Mod.Ts = reader["Ts"].ToString();
stu.Add(Mod);
}
}
}
return stu;
}
}
public class Bll
{
public Dal dal = new Dal();
public List<Model> GetAll()
{
return dal.GetAll().Count > 0 ? dal.GetAll() : null;
}
public List<Model> GetAll1()
{
return dal.GetAll1().Count > 0 ? dal.GetAll1() : null;
}
}
public class Model
{
public int ID { get; set; }
public string Name { get; set; }
public string One { get; set; }
public string Two { get; set; }
public string Three { get; set; }
public string Four { get; set; }
public string Five { get; set; }
public string Fq{ get; set; }
public string Lw{ get; set; }
public string Zg{ get; set; }
public string Yg{ get; set; }
public string Fs{ get; set; }
public string Cs{ get; set; }
public string Ts{ get; set; }
}
public static class SqlHelper
{
//获取连接字符串
private static readonly string constr = ConfigurationManager.ConnectionStrings["connectionStr"].ConnectionString;
//ExecuteNonQuery()方法 执行增删改
public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
//ExecuteScalar()方法 执行返回单个值
public static object ExecuteScalar(string sql, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteScalar();
}
}
}
//ExecuteReader()方法 执行返回SqlDataReader
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
{
SqlConnection con = new SqlConnection(constr);
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;
}
}
}
//ExecuteDataTable()方法 执行返回
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, constr))
{
if (pms != null)
{
adapter.SelectCommand.Parameters.AddRange(pms);
}
adapter.Fill(dt);
}
return dt;
}
}
运行结果:
关于测试会在今后几天进行处理。