好久没复习了,以前学的,不复习的话,会遗忘,所以还是多复习多学习!废话不多说,开始~~~~
首先数据库脚本:
USE [DB_MyStudentLife]GO/****** Object: Table [dbo].[MyClass] Script Date: 11/26/2015 22:19:31 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[MyClass]( [C_ID] [INT] NOT NULL, [C_Name] [NVARCHAR](200) NOT NULL, [C_Descr] [NVARCHAR](MAX) NOT NULL,PRIMARY KEY CLUSTERED ( [C_ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO
然后是实体层代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Entity{ public class ClassEntity { /// <summary> /// 班级编号 /// </summary> public int CID { get; set; } /// <summary> /// 班级名称 /// </summary> public string CName { get; set; } /// <summary> /// 班级描述 /// </summary> public string CDescription { get; set; } }}
然后是DAL层:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Configuration;using System.Data;using System.Data.SqlClient;namespace DAL{ public class SQLHelper { /// <summary> /// 获取连接字符串 /// </summary> public static string ConnectionString { get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; } } /// <summary> /// 获取Datatable数据 /// </summary> /// <param name="sql">SQL语句</param> /// <param name="type">命令类型</param> /// <param name="param">参数列表</param> /// <returns>返回DataTable类型</returns> public static DataTable GetDataTable(string sql,CommandType type,params SqlParameter[] param) { //创建连接对象 using (SqlConnection conn = new SqlConnection(ConnectionString)) { //创建数据适配器对象 using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn)) { if(param!=null) { sda.SelectCommand.Parameters.AddRange(param); } sda.SelectCommand.CommandType = type; DataTable table = new DataTable(); sda.Fill(table); return table; } } } }}
using Entity;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;using System.Data.SqlClient;namespace DAL{ public class ClassDAL { /// <summary> /// 获取班级列表数据 /// </summary> /// <returns></returns> public List<ClassEntity> GetList() { string sql = "SELECT * FROM dbo.MyClass;"; DataTable table = SQLHelper.GetDataTable(sql, CommandType.Text); //此处不能直接new一个对象 List<ClassEntity> classListModel = null; //table不为空 if (table.Rows.Count > 0) { //要在这里new ,创建对象 classListModel = new List<ClassEntity>(); ClassEntity model = null; foreach (DataRow row in table.Rows) { model = new ClassEntity(); //加载数据 LoadEntity(row, model); classListModel.Add(model); } } return classListModel; } /// <summary> /// 加载数据 /// </summary> /// <param name="row"></param> /// <param name="model"></param> public void LoadEntity(DataRow row, ClassEntity model) { if (row["C_ID"] != null) { model.CID = Convert.ToInt32(row["C_ID"]); } if (row["C_Name"] != null) { model.CName = row["C_Name"].ToString(); } if (row["C_Descr"] != null) { model.CDescription = row["C_Descr"].ToString(); } } }}
然后是BLL层:
using DAL;using Entity;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BLL{ public class ClassBLL { ClassDAL dal = new ClassDAL(); /// <summary> /// 获取Class列表 /// </summary> /// <returns></returns> public List<ClassEntity> GetList() { return dal.GetList(); } }}
然后是Web项目:
using BLL;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace Web{ public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ClassBLL bll = new ClassBLL(); GridView1.DataSource= bll.GetList(); GridView1.DataBind(); } }}
效果图:
DAL层的代码,表红色的部分,需要注意,如果实例化的位置出错了,就会得到下面的结果: