Simple Layer-3 Review, layer-3 Review

Source: Internet
Author: User

Simple Layer-3 Review, layer-3 Review

I haven't reviewed it for a long time. I learned it before. If I don't review it, I will forget it. So I should review more and learn more! Not to mention nonsense. Start ~~~~

First, the database script:

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

Then there is the entity Layer Code:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Entity {public class ClassEntity {// <summary> // class ID /// </summary> public int CID {get; set ;} /// <summary> /// class name /// </summary> public string CName {get; set ;} /// <summary> /// class description /// </summary> public string CDescription {get; set ;}}}

Then the DAL layer:

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> // obtain the connection string /// </summary> public static string ConnectionString {get {return ConfigurationManager. connectionStrings ["ConnectionString"]. connectionString ;}}// /<Summary> // obtain the Datatable data // </summary> // <param name = "SQL"> SQL statement </param> // <param name = "type"> command type </param> /// <param name = "param"> parameter list </param> /// <returns> returns the DataTable type </returns> public static DataTable GetDataTable (string SQL, commandType type, params SqlParameter [] param) {// create the connection object using (SqlConnection conn = new SqlConnection (ConnectionString) {// create the data adapter object 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. 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> /// obtain the class List data /// </summary> /// <returns> </returns> public List <classEntity> GetList () {string SQL = "SELECT * FROM dbo. myClass; "; DataTable table = SQLHelper. getDataTable (SQL, CommandType. text); // you cannot directly create an object List <ClassEntity> classListModel = null; // The table is not empty if (table. rows. count> 0) {// here new is to be created. The object classListModel = new List <ClassEntity> (); ClassEntity model = null; foreach (DataRow row in table. rows) {model = new ClassEntity (); // load the data LoadEntity (row, model); classListModel. add (model) ;}}return classListModel ;}/// <summary> /// load data /// </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 ();}}}}

Then there is the BLL layer:

Using DAL; using Entity; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace BLL {public class ClassBLL {ClassDAL dal = new ClassDAL (); /// <summary> /// obtain the Class List /// </summary> /// <returns> </returns> public List <ClassEntity> GetList () {return dal. getList ();}}}

Then there is a Web project:

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();        }    }}

:

The code at the DAL layer and the red part of the table. Note that if an error occurs in the instantiation position, the following result is displayed:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.