CourseDAL/ChapterService.cs

(完整代码) 2KB

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Course.Models;

namespace Course.DAL
{
    public class ChapterService
    {
        /// <summary>
        /// 根据 BookId 获取得 Chapter集合
        /// </summary>
        /// <param name="bookId">书的ID</param>
        /// <returns></returns>
        public List<Chapter> GetChaptersByBookId(int bookId)
        {
            List<Chapter> chapterList = new List<Chapter>();
            using (SqlConnection conn = new SqlConnection(DBHelper.conStr))
            {
                SqlCommand cmd = new SqlCommand("usp_GetChapterByBookID", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@bookID", SqlDbType.Int).Value = bookId;
                conn.Open();
                using (SqlDataReader objReader = cmd.ExecuteReader())
                {
                    Chapter chapter = null;
                    while (objReader.Read())
                    {
                        chapter = new Chapter();
                        chapter = this.GetChapterBySqlDataReader(objReader);
                        chapterList.Add(chapter);
                    }
                    objReader.Close();
                }
            }
            return chapterList;
        }

        /// <summary>
        /// 根据 SqlDataReader 返回 Chapter 对象
        /// </summary>
        /// <param name="reader">SqlDataReader reader</param>
        /// <returns></returns>
        public Chapter GetChapterBySqlDataReader(SqlDataReader reader)
        {
            Chapter chapter = new Chapter();
            chapter.ChapterId = Convert.ToInt32(reader["chapterID"].ToString());
            chapter.ChapterName = reader["chapterName"].ToString();
            chapter.Hours = Convert.ToInt32(reader["hours"].ToString());
            chapter.BookId = Convert.ToInt32(reader["bookID"].ToString());
            return chapter;
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。