三层架构实例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
这里以查询数据库中student表的所有信息为例:
1、模型层,Student.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SchoolModels
{
public class Student
{
public Student()
{ }
public Student(int id, string name, string pwd, int age, string sex) {
this.StudentId = id;
this.StudentName = name;
this.StudentPwd = pwd;
}
private int studentId;
public int StudentId
{
get { return studentId; }
set { studentId = value; }
}
private string studentName;
public string StudentName
{
get { return studentName; }
set { studentName = value; }
}
private string studentPwd;
public string StudentPwd
{
get { return studentPwd; }
set { studentPwd = value; }
}
}
}
2、数据访问层,StudentService.cs文件:using System.Text;
using System.Data;
using System.Data.SqlClient;
using SchoolModels;
namespace SchoolDal
{
public class StudentService
{
public static IList
{
List
string sql = "select * from student";
SqlCommand cmd = new SqlCommand(sql, DBHelper.con);
DBHelper.con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Student student = new Student();
student.StudentId = (int)reader["StudentId"];
student.StudentName = reader["StudentName"].ToString();
student.StudentPwd = reader["StudentPwd"].ToString();
stus.Add(student);
}
reader.Close();
DBHelper.con.Close();
return stus;
}
}
其中,里面有一个DBHelper.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using SchoolModels;
using System.Data;
using System.Data.SqlClient;
namespace SchoolDal
{
public class DBHelper
{
public static string conStr = ConfigurationManager.ConnectionStrings["conDb"].ToString();
public static SqlConnection con = new SqlConnection(conStr); }
}
3、业务逻辑层,StudentManager.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolDal;
using SchoolModels;
namespace SchoolBll
{
public class StudentManager
{
public static IList
{
return StudentService.GetAllStudents();
}
}
}