您的位置:首页 > 其它

使用DataReader读取数据即查询所有学生

2013-06-07 08:56 267 查看
一:这次主要整理上次课在学生信息管理中添加一个学生列表,并显示处理

即在



显示如下:



二:具体操作如下:

首先在数据访问层添加查询所有学生的功能:

代码如下:

//查询所有学生
public List<Student> getAll() {

List<Student> list = new List<Student>();
string s = "server=.;database=SampleDb;Integrated Security=true;";
SqlConnection conn = new SqlConnection(s);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from  student";
cmd.Connection = conn;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
Student student = new Student();
student.Id = (string)reader["id"];
student.Name = (string)reader["name"];
student.Gender = (string)reader["gender"];
student.Major = (string)reader["major"];
student.Grade = (int)reader["grade"];
student.TheClass = (int)reader["class"];
list.Add(student);
}
reader.Close();
conn.Close();
return list;
}


为啦便于代码的复用,特抽出一个DbHelper类,实现与数据库的连接:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace MySchoolDAL
{
public class DbHelper
{
public static void myExecute(string sql)
{
string s = "server=.;database=SampleDb;Integrated Security=true;";
SqlConnection conn = new SqlConnection(s);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

}
}


然后在业务逻辑层调用数据访问层的显示所有学生的功能:

public List<Student> getALl() {
StudentDal dal = new StudentDal();
return dal.getAll();

}


最后在用户界面层学生列表窗口下,双击button1,实现调用业务逻辑层:

private void 查看_Click(object sender, EventArgs e)
{
StudentBll bll = new StudentBll();
List<Student> list = bll.getALl();
gride.DataSource = list;
}


还忘啦一点就是,调用get set方法,在实体类中封装字段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySchoolEntity
{
public  class Student
{
public string Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Major { get; set; }
public int Grade { get; set; }
public int TheClass { get; set; }
}
}




为了提高代码的复用,顾将studentDal.cs与数据库连接的代码放到DbHelper.cs中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MySchoolEntity;
namespace MySchoolDAL
{
public class StudentDal
{
public void insert(Student student)
{

string sql = string.Format("insert into Student(Id,Name,Gender,Major,Grade,Class) values('{0}','{1}','{2}','{3}',{4},{5})",
student.Id, student.Name,student.Major, student.Gender, student.Grade, student.TheClass);
DbHelper.myExecute(sql);
}
public void delete(string id)
{
string sql = string.Format("delete from student where id='{0}'", id);
DbHelper.myExecute(sql);

}
//查询所有学生
public List<Student> getAll() {

List<Student> list = new List<Student>();
SqlConnection conn = DbHelper.createConnection();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from  student";
cmd.Connection = conn;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
Student student = new Student();
student.Id = (string)reader["id"];
student.Name = (string)reader["name"];
student.Gender = (string)reader["gender"];
student.Major = (string)reader["major"];
student.Grade = (int)reader["grade"];
student.TheClass = (int)reader["class"];
list.Add(student);
}
reader.Close();
conn.Close();
return list;
}

}
}


DbHelper.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace MySchoolDAL
{
public class DbHelper
{

public static void myExecute(string sql)
{

SqlConnection conn = new SqlConnection();
conn.ConnectionString = connStr;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

public static SqlConnection createConnection()
{ string connStr = "server=.;database=SampleDb;Integrated Security=true;";
return new SqlConnection(connStr);

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐