[C#學(xué)習(xí)筆記28]項目實戰(zhàn)之課程管理系統(tǒng)開發(fā)的項目框架搭建與準(zhǔn)備工作
課程管理系統(tǒng)項目框架搭建
一、項目需求分析
????根據(jù)需求分析設(shè)計好項目的功能
二、項目框架的選擇
????1、小型項目可以根據(jù)需要選擇兩層或三層結(jié)構(gòu)
????2、中大型項目至少是三層架構(gòu)和其他架構(gòu)組合
三、框架的搭建
????1、UI設(shè)計好
????2、添加需要的模塊(Models、DAL、BLL)
添加類庫CourseManageModels、CourseManageDAL、CourseManageBLL
????????1)CourseManageModels實體類包括Course.cs、CourseCategory.cs、Teacher.cs

結(jié)合數(shù)據(jù)庫寫實體類:
文件Course.cs內(nèi)容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CourseManageModels
{
? ? /// <summary>
? ? /// 課程實體類
? ? /// </summary>
? ? [Serializable]//序列化
? ? public class Course
? ? {
? ? ? ? public int CourseId { get; set; }
? ? ? ? public string CourseName { get; set; }
? ? ? ? public string CourseContent { get; set; }
? ? ? ? public int ClassHour { get; set; }
? ? ? ? public int Credit { get; set; }
? ? ? ? public int CategoryId { get; set; }
? ? ? ? public int TeacherId { get; set; }
? ? }
}

????????
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CourseManageModels
{
? ? public class CourseCategory
? ? {
? ? ? ? public int CategoryId { get; set; }
? ? ? ? public string CategoryName{ get; set; }
? ? }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CourseManageModels
{
? ? public class Teacher
? ? {
? ? ? ? public int TeacherId { get; set; }
? ? ? ? public string LoginAccount { get; set; }
? ? ? ? public string LoginPwd { get; set; }
? ? ? ? public string TeacherName { get; set; }
? ? ? ? public string PhoneNumber { get; set; }
? ? ? ? public string NowAddress { get; set; }
? ? }
}

????2)CourseManageDAL添加數(shù)據(jù)訪問類包括CourseService.cs、CourseCategoryService.cs、TeacherService.cs、Helper\SQLHelper.cs(通用數(shù)據(jù)訪問類)
????3)CourseManageBLL添加業(yè)務(wù)邏輯類包括CourseManager.cs、CourseCategoryManager.cs、TeacherManager.cs

3、添加模塊之間的引用,添加完成后重新生成解決方案。
????1)DAL添加引用System.Configuration用于讀取連接數(shù)據(jù)庫的配置文件和CourseManageModels(在引用管理器的項目中),并在SQLHelper.cs添加命名空間
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
????2)BLL添加引用CourseManageModels、CourseManageDAL
????3)UI添加引用CourseManageBLL、CourseManageModels
4、通用數(shù)據(jù)訪問類準(zhǔn)備好
SQLHelper.cs內(nèi)容如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace CourseManageDAL
{
? ? /// <summary>
? ? /// 通用數(shù)據(jù)訪問類
? ? /// </summary>
? ? public class SQLHelper
? ? {
? ? ? ? private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();
? ? ? ? /// <summary>
? ? ? ? /// 執(zhí)行insert、update、delete類型的SQL語句
? ? ? ? /// </summary>
? ? ? ? /// <param name="sql"></param>
? ? ? ? /// <returns>受影響的行數(shù)</returns>
? ? ? ? public static int Update(string sql)
? ? ? ? {
? ? ? ? ? ? SqlConnection conn = new SqlConnection(connString);
? ? ? ? ? ? SqlCommand cmd = new SqlCommand(sql,conn);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conn.Open();
? ? ? ? ? ? ? ? return cmd.ExecuteNonQuery();
? ? ? ? ? ? }
? ? ? ? ? ? catch ( Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //捕獲ex對象相關(guān)信息并保存到日志文件中...
? ? ? ? ? ? ? ? throw new Exception("執(zhí)行public static int Update(string sql)發(fā)生異常:"+ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? finally
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conn.Close();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 執(zhí)行單一結(jié)果查詢
? ? ? ? /// </summary>
? ? ? ? /// <param name="sql"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public static object GetSingleResult(string sql)
? ? ? ? {
? ? ? ? ? ? SqlConnection conn = new SqlConnection(connString);
? ? ? ? ? ? SqlCommand cmd = new SqlCommand(sql, conn);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conn.Open();
? ? ? ? ? ? ? ? return cmd.ExecuteScalar();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //捕獲ex對象相關(guān)信息并保存到日志文件中...
? ? ? ? ? ? ? ? throw new Exception("執(zhí)行public static object GetSingleResult(string sql)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? finally
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conn.Close();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 執(zhí)行一個結(jié)果集的查詢
? ? ? ? /// </summary>
? ? ? ? /// <param name="sql"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public static SqlDataReader GetReader(string sql)
? ? ? ? {
? ? ? ? ? ? SqlConnection conn = new SqlConnection(connString);
? ? ? ? ? ? SqlCommand cmd = new SqlCommand(sql, conn);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conn.Open();
? ? ? ? ? ? ? ? return cmd.ExecuteReader(CommandBehavior.CloseConnection);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //捕獲ex對象相關(guān)信息并保存到日志文件中...
? ? ? ? ? ? ? ? throw new Exception("執(zhí)行public static SqlDataReader GetReader(string sql)發(fā)生異常:" + ex.Message);
? ? ? ? ? ? }
? ? ? ? ? ? finally
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conn.Close();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
5、一定要重新生成解決方案