diff --git "a/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..a97987551c7710a61ae31e3149aa6ee1765e739c --- /dev/null +++ "b/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,277 @@ +## 学生管理系统 + +### 1. 模型定义 (`Student.cs`) + +**功能描述**: 模型类 `Student` 代表了数据库中的 `Students` 表结构,包含三个字段:`Id`(主键)、`StudentCode`(学号)和 `StudentName`(姓名)。 + +```c# +namespace StudentManager.Models; + +// 定义 Student 模型类 +public class Student +{ + public int Id { get; set; } // 主键 ID + public string StudentCode { get; set; } = null!; // 学号,不能为空 + public string StudentName { get; set; } = null!; // 姓名,不能为空 +} +``` + +------ + +### 2. 数据库上下文配置 (`StudentDbContext.cs`) + +**功能描述**: + 通过继承 `DbContext` 类,实现数据库访问的基础配置。`StudentDbContext` 包含三个 `DbSet`:`Students`、`Courses` 和 `Scores`,分别对应数据库中的三张表。 + +#### 数据库连接配置 + +数据库通过 `OnConfiguring` 方法进行连接。配置时需要: + +1. 指定数据库服务器地址和凭据。 +2. 安装 `Entity Framework Core` 和 `SQL Server` 包以支持相关功能。 +3. 使用迁移命令将模型更新到数据库中。 + +```c# +using Microsoft.EntityFrameworkCore; +namespace StudentManager.Models; + +// 配置数据库上下文 +public class StudentDbContext : DbContext +{ + // 定义数据库表对应的 DbSet + public DbSet Students { get; set; } = null!; + public DbSet Courses { get; set; } = null!; + public DbSet Scores { get; set; } = null!; + + // 配置数据库连接字符串 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var str = "示例字符串"; + optionsBuilder.UseSqlServer(str); + } +} +``` + +**迁移命令及依赖包说明**: + +- 安装 `EF Core` 相关依赖包: + + ```bash + dotnet add package Microsoft.EntityFrameworkCore.SqlServer + dotnet add package Microsoft.EntityFrameworkCore.Design + ``` + +- 安装 `EF` 工具(用于执行迁移命令): + + ```bash + dotnet tool install --global dotnet-ef + ``` + +- 执行迁移命令: + + 1. 生成迁移文件: + + ```bash + dotnet ef migrations add InitialCreate + ``` + + 2. 更新数据库: + + ```bash + dotnet ef database update + ``` + +------ + +### 3. 视图文件 + +#### **3.1 列表页 (`Index.cshtml`)** + +功能:显示学生列表,并支持通过关键字搜索学生信息。 + +```html +
+
+ + +
+ 新增 +
+
+ +
+ + + + + + + + @foreach (var item in Model) + { + + + + + + + } +
Id学号姓名操作
@item.Id@item.StudentCode@item.StudentName +
+ 编辑 +
+
+ 删除 +
+
+
+
+``` + +------ + +#### **3.2 新增页 (`Create.cshtml`)** + +功能:用于新增学生信息。 + +```html +@model Student + +
+
+
+ +
+``` + +------ + +#### **3.3 删除页 (`Delete.cshtml`)** + +功能:显示即将删除的学生信息,并提供删除确认操作。 + +```html + + + + + + + + + + + + + +
姓名:@Model.StudentName
学号:@Model.StudentCode
+ 删除 + + 回到主页 +
+``` + +------ + +#### **3.4 编辑页 (`Edit.cshtml`)** + +功能:显示待修改的学生信息,并允许编辑。 + +```html +@model Student + +
+
+
+ +
+``` + +------ + +### 4. 控制器功能实现 + +以下是核心增删改查功能的控制器代码。 + +#### **4.1 增** + +```c# +public IActionResult Create() +{ + return View(); +} + +[HttpPost] +public IActionResult Create(Student input) +{ + _db.Students.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); +} +``` + +#### **4.2 删** + +```c# +public IActionResult Delete(int id) +{ + var student = _db.Students.FirstOrDefault(x => x.Id == id); + return View(student); +} + +public IActionResult DeleteConfirm(int id) +{ + var student = _db.Students.FirstOrDefault(x => x.Id == id); + if (student != null) + { + _db.Students.Remove(student); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + return NotFound(); +} +``` + +#### **4.3 改** + +```c# +public IActionResult Edit(int id) +{ + var student = _db.Students.FirstOrDefault(x => x.Id == id); + return View(student); +} + +[HttpPost] +public IActionResult Edit(Student input) +{ + var student = _db.Students.FirstOrDefault(x => x.Id == input.Id); + if (student != null) + { + student.StudentCode = input.StudentCode; + student.StudentName = input.StudentName; + _db.SaveChanges(); + return RedirectToAction("Index"); + } + return NotFound(); +} +``` + +#### **4.4 查** + +```c# +public IActionResult Index(string keyword) +{ + keyword = keyword?.Trim() ?? string.Empty; + var students = _db.Students + .Where(x => x.StudentName.Contains(keyword) || x.StudentCode.Contains(keyword)) + .ToList(); + return View(students); +} +``` \ No newline at end of file diff --git "a/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237 (2).md" "b/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237 (2).md" new file mode 100644 index 0000000000000000000000000000000000000000..776c865540d51d24cd045cf1351ac578fae59d61 --- /dev/null +++ "b/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237 (2).md" @@ -0,0 +1,230 @@ +### 成绩管理系统 + +### 数据库配置 + +采用 `Entity Framework Core` 实现数据库交互。通过构造函数将 `StudentDbContext` 引入到控制器中,用于访问 `Students`、`Courses` 和 `Scores` 表。 + +```c# +// 构造函数中初始化数据库上下文 +private readonly StudentDbContext _db; + +public ScoresController() +{ + // 初始化数据库上下文 + _db = new StudentDbContext(); +} +``` + +------ + +### 1. 增加功能 + +新增功能包含两部分:显示新增页面和提交新增数据。 + +#### (1)获取新增页面 + +**功能描述**: + 当用户访问新增页面时,后端将 `Students` 和 `Courses` 表中的数据加载到前端的下拉框中,供用户选择。 + +```c# +public IActionResult Create() +{ + // 获取 Students 和 Courses 数据,用于生成下拉框 + var students = _db.Students.ToList(); + var courses = _db.Courses.ToList(); + + // 使用 SelectList 将数据绑定到下拉框 + ViewBag.Students = new SelectList(students, "Id", "StudentName"); + ViewBag.Courses = new SelectList(courses, "Id", "CourseName"); + + // 返回新增视图 + return View(); +} +``` + +#### (2)提交新增数据 + +**功能描述**: + 通过 `HttpPost` 方法接收用户提交的表单数据,将其保存到 `Scores` 表中,并跳转回列表页面。 + +```c# +[HttpPost] +public IActionResult Create(Score input) +{ + // 添加新数据到 Scores 表 + _db.Scores.Add(input); + // 保存更改到数据库 + _db.SaveChanges(); + // 重定向到列表页 + return RedirectToAction("Index"); +} +``` + +------ + +### 2. 删除功能 + +删除功能包含两部分:显示删除页面和确认删除。 + +#### (1)获取删除页面 + +**功能描述**: + 根据 `Id` 查询需要删除的数据,并通过联表查询,将 `StudentName` 和 `CourseName` 一并传递给前端,供用户确认删除。 + +```c# +public IActionResult Delete(int id) +{ + // 根据 ID 查询 Score 数据 + var score = _db.Scores.FirstOrDefault(x => x.Id == id); + if (score == null) + { + return NotFound(); + } + + // 联表查询 Student 和 Course 信息 + var student = _db.Students.FirstOrDefault(x => x.Id == score.StudentId); + var course = _db.Courses.FirstOrDefault(x => x.Id == score.CourseId); + + var model = new + { + score.Id, + StudentName = student?.StudentName ?? "", // 如果为空,默认返回空字符串 + CourseName = course?.CourseName ?? "", + score.Scores + }; + + // 返回删除视图 + return View(model); +} +``` + +#### (2)确认删除 + +**功能描述**: + 用户确认后,根据 `Id` 删除对应记录,并保存更改到数据库中。如果记录不存在,则返回 404 页面。 + +```c# +public IActionResult DeleteConfirm(int id) +{ + // 根据 ID 查询记录 + var score = _db.Scores.FirstOrDefault(x => x.Id == id); + if (score != null) + { + // 从 Scores 表中删除 + _db.Scores.Remove(score); + // 保存更改到数据库 + _db.SaveChanges(); + // 重定向到列表页 + return RedirectToAction("Index"); + } + return NotFound(); +} +``` + +------ + +### 3. 修改功能 + +修改功能包含两部分:显示编辑页面和提交编辑数据。 + +#### (1)获取编辑页面 + +**功能描述**: + 通过 `Id` 查询需要编辑的数据,同时加载所有学生和课程信息,以便在页面上显示下拉框供选择。 + +```c# +public IActionResult Edit(int id) +{ + // 根据 ID 查询 Score 数据 + var score = _db.Scores.FirstOrDefault(x => x.Id == id); + if (score == null) + { + return NotFound(); + } + + // 获取下拉框数据 + var students = _db.Students.ToList(); + var courses = _db.Courses.ToList(); + + // 绑定下拉框数据 + ViewBag.Students = new SelectList(students, "Id", "StudentName"); + ViewBag.Courses = new SelectList(courses, "Id", "CourseName"); + + // 返回编辑视图 + return View(score); +} +``` + +#### (2)提交编辑数据 + +**功能描述**: + 接收用户修改后的数据,更新到数据库中,并保存更改。如果记录不存在,则返回 404 页面。 + +```c# +[HttpPost] +public IActionResult Edit(Score input) +{ + // 根据 ID 查找对应数据 + var score = _db.Scores.FirstOrDefault(x => x.Id == input.Id); + if (score != null) + { + // 更新数据 + score.StudentId = input.StudentId; + score.CourseId = input.CourseId; + score.Scores = input.Scores; + + // 保存更改到数据库 + _db.Scores.Update(score); + _db.SaveChanges(); + + return RedirectToAction("Index"); + } + return NotFound(); +} +``` + +------ + +### 4. 查询功能 + +查询功能通过在列表页面中加载所有成绩信息,并支持按关键字搜索。 + +#### 列表页面查询 + +**功能描述**: + 从 `Scores` 表中加载数据,通过联表查询,将学生姓名和课程名称一并整合到结果中,并支持通过关键字搜索过滤结果。 + +```c# +public IActionResult Index(string keyword) +{ + var students = _db.Students.ToList(); + var courses = _db.Courses.ToList(); + var scores = _db.Scores.ToList(); + + // 联表查询,整合数据 + var result = scores.Select(x => + { + var student = students.FirstOrDefault(y => y.Id == x.StudentId); + var course = courses.FirstOrDefault(y => y.Id == x.CourseId); + + return new + { + x.Id, + StudentName = student?.StudentName ?? "", // 如果为空,默认返回空字符串 + CourseName = course?.CourseName ?? "", + x.Scores + }; + }); + + // 处理搜索关键字 + keyword = keyword?.Trim() ?? ""; + if (!string.IsNullOrEmpty(keyword)) + { + // 根据关键字过滤数据 + result = result.Where(x => x.StudentName.Contains(keyword) || x.CourseName.Contains(keyword)); + } + + // 返回视图,显示查询结果 + return View(result); +} +``` \ No newline at end of file diff --git "a/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..ee9322a8c89d3527de204c4ec913ce354d529e31 --- /dev/null +++ "b/\345\217\266\345\255\220\350\241\241/\344\275\234\344\270\232/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,228 @@ +## 成绩管理系统实现 + +### 1. 创建模型 —— `Score.cs` + +定义一个 `Score` 模型,用于存储学生成绩相关数据。 + +```csharp +namespace StudentManager.Models; + +public class Score +{ + public int Id { get; set; } + public int StudentId { get; set; } + public int CourseId { get; set; } + public decimal Scores { get; set; } +} +``` + +------ + +### 2. 创建数据库上下文 —— `StudentDbContext.cs` + +配置数据库上下文,包含学生、课程和成绩的数据表。 + +```csharp +using Microsoft.EntityFrameworkCore; + +namespace StudentManager.Models; + +public class StudentDbContext : DbContext +{ + public DbSet Students { get; set; } = null!; + public DbSet Courses { get; set; } = null!; + public DbSet Scores { get; set; } = null!; + + // 配置数据库连接 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var connectionString = "示例字符串"; + optionsBuilder.UseSqlServer(connectionString); + } +} +``` + +**依赖包与迁移命令**: + +1. 安装依赖包: + + ```bash + dotnet add package Microsoft.EntityFrameworkCore.SqlServer + dotnet add package Microsoft.EntityFrameworkCore.Design + ``` + +2. 安装 EF 工具: + + ```bash + dotnet tool install --global dotnet-ef + ``` + +3. 生成迁移文件: + + ```bash + dotnet ef migrations add InitialCreate + ``` + +4. 更新数据库: + + ```bash + dotnet ef database update + ``` + +------ + +### 3. 创建视图 —— `Index.cshtml` + +列出所有学生成绩,并提供查询、新增、编辑和删除操作。 + +```html +
+
+ + + +
+ 新增 +
+
+
+ + + + + + + + + @foreach (var item in Model) + { + + + + + + + + } +
Id姓名课程成绩操作
@item.Id@item.StudentName@item.CourseName@item.Scores + 编辑 | + 删除 +
+
+
+``` + +------ + +### 4. 增加 —— `Create.cshtml` + +提供新增成绩的表单。 + +```html +@model StudentManager.Models.Score + +
+ + + + + + + + + + + + + + + + + +
学生姓名:
课程名称:
成绩:
取消
+
+``` + +------ + +### 5. 删除 —— `Delete.cshtml` + +确认是否删除指定成绩。 + +```html +@model StudentManager.Models.Score + + + + + + + + + + + + + + + + + + +
姓名:@Model.StudentName
课程:@Model.CourseName
成绩:@Model.Scores
删除取消
+``` + +------ + +### 6. 修改 —— `Edit.cshtml` + +提供编辑成绩的表单。 + +```html +@model StudentManager.Models.Score + +
+ + + + + + + + + + + + + + + + + +
学生姓名:
课程名称:
成绩:
取消
+
+``` + +------ + +### 7. 查询 —— 在 `Index.cshtml` 中实现 + +通过关键字查询成绩。 + +```html + +``` + +------ + +通过以上步骤,您可以实现一个基本的成绩管理系统。 \ No newline at end of file