做了些小笔记
数据访问层
private readonly string connString = ConfigurationManager.ConnectionStrings["MySchoolConnectionString"].ToString();
public List<Student> GetAllStudents()
{
List<Student> students = new List<Student>();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand command = new SqlCommand("usp_SelectStudentsAll", conn);
command.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
Student student = new Student();
student.LoginId = Convert.ToString(reader["LoginId"]);
student.StudentName = Convert.ToString(reader["StudentName"]);
student.StudentNo = Convert.ToString(reader["StudentNO"]);
student.Sex = Convert.ToString(reader["Sex"]);
student.Phone = Convert.ToString(reader["Phone"]);
students.Add(student);
}
}
conn.Close();
}
return students;
}
表示层
private void StudentListForm_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = studentManager.GetAllStudents();
}
数据访问层
private readonly string connString = ConfigurationManager.ConnectionStrings["MySchoolConnectionString"].ToString();
public List<Student> GetAllStudents()
{
List<Student> students = new List<Student>();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand command = new SqlCommand("usp_SelectStudentsAll", conn);
command.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
Student student = new Student();
student.LoginId = Convert.ToString(reader["LoginId"]);
student.StudentName = Convert.ToString(reader["StudentName"]);
student.StudentNo = Convert.ToString(reader["StudentNO"]);
student.Sex = Convert.ToString(reader["Sex"]);
student.Phone = Convert.ToString(reader["Phone"]);
students.Add(student);
}
}
conn.Close();
}
return students;
}
表示层
private void StudentListForm_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = studentManager.GetAllStudents();
}