⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 courseservice.cs

📁 在线考试系统完整版源码(C#&Access) 该考试系统为一同学毕业设计
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using ExamModel.Auto_Generated_Code;
//该源码下载自www.51aspx.com(51aspx.com)

namespace ExamDAL.Auto_Generated_Code
{
    public static class CourseService
    {
        //添加科目方法
        public static int AddCourse(Course course)
        {
            string sql = "insert into Course(DepartmentID,CourseName) values (@DepartmentID,@Coursename)";
            OleDbParameter[] para = new OleDbParameter[]
            {
                new OleDbParameter("@DepartmentID",course.DepartmentID.DepartmentID),//FK
                new OleDbParameter("@CourseName",course.CName)
            };
            return DBHelp.ExecuteCommand(sql, para);
        }

        //能过ID号查询科目 返回Course对象
        public static Course GetCourseById(int id)
        {
            string sql = "select * from Course where CourseID=@id";
            int departmentId;
            try
            {
                OleDbDataReader reader = DBHelp.GetReader(sql, new OleDbParameter("@id", id));
                if (reader.Read())
                {
                    Course course = new Course();
                    course.CID = (int)reader["CourseID"];
                    departmentId = (int)reader["DepartmentID"];//FK
                    course.CName = (string)reader["CourseName"];

                    reader.Close();
                    course.DepartmentID = DepartmentService.GetDepartmentById(departmentId);

                    return course;
                }
                else
                {
                    reader.Close();
                    return null;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
               
            }

        }
       
        //查所有的科目 返回DataSet
        public static DataSet GetCourseList()
        {
            string sql = "select * from Course";
            return DBHelp.GetDataSet(sql);
        }
      
        //查询所有科目
        public static IList<Course> GetAllCourses()
        {
            string sqlAll = "select * from Course";
            return GetCoursesBySql(sqlAll);
        }
        //根据SQL语句查询科目
        private static IList<Course> GetCoursesBySql(string safeSql)
        {
            List<Course> list = new List<Course>();
            using (DataSet table = DBHelp.GetDataSet(safeSql))
            {
                foreach (DataRow row in table.Tables[0].Rows)
                {
                    Course course = new Course();
                    course.CID = (int)row["CourseID"];
                    course.CName = (string)row["CourseName"];
                    course.DepartmentID = DepartmentService.GetDepartmentById((int)row["DepartmentID"]);//FK
                    list.Add(course);
                }
                return list;

            }
        }
        //通过科目名称获得科目ID
        public static Course GetCourseByString(string str)
        {
            string sql = "select * from Course where CourseName=@courseName";

            int departmentId;//系ID

            try
            {
                OleDbDataReader reader = DBHelp.GetReader(sql, new OleDbParameter("@courseName", str));
                if (reader.Read())
                {
                    Course course = new Course();

                    course.CID = (int)reader["CourseID"];
                    course.CName = str;
                    departmentId = (int)reader["DepartmentID"];//FK
                    reader.Close();

                    course.DepartmentID = DepartmentService.GetDepartmentById(departmentId);
                    return course;
                }
                else
                {
                    reader.Close();
                    return null;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        //更新科目
        public static void ModifyCourse(Course course)
        {     
            try
            {
                string sql1 = "update Course set CourseName='" + course.CName + "' where CourseID=" + course.CID;
                DBHelp.ExecuteCommand(sql1);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        //删除科目
        public static void DeleteBookById(int id)
        {
            string sql = "Delete from Course where CourseID=" + id;
            try
            {
                DBHelp.ExecuteCommand(sql);
         
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -