文章出處

好久沒復習了,以前學的,不復習的話,會遺忘,所以還是多復習多學習!廢話不多說,開始~~~~

首先數據庫腳本:

USE [DB_MyStudentLife]
GO

/****** Object:  Table [dbo].[MyClass]    Script Date: 11/26/2015 22:19:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE 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層的代碼,表紅色的部分,需要注意,如果實例化的位置出錯了,就會得到下面的結果:


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()