From 5e12701ebf2c987aadfbf71292f8293fbfe54336 Mon Sep 17 00:00:00 2001 From: wengxindong <2092619391@qq.com> Date: Sun, 29 Dec 2024 20:51:02 +0800 Subject: [PATCH] 12-29tijiao --- ...23\345\255\246\347\224\237\350\241\250.md" | 251 ++++++++++++++++++ ...25\346\210\220\347\273\251\350\241\250.md" | 251 ++++++++++++++++++ 2 files changed, 502 insertions(+) create mode 100644 "\347\277\201\344\277\241\346\240\213/20241223\345\255\246\347\224\237\350\241\250.md" create mode 100644 "\347\277\201\344\277\241\346\240\213/20241225\346\210\220\347\273\251\350\241\250.md" diff --git "a/\347\277\201\344\277\241\346\240\213/20241223\345\255\246\347\224\237\350\241\250.md" "b/\347\277\201\344\277\241\346\240\213/20241223\345\255\246\347\224\237\350\241\250.md" new file mode 100644 index 0000000..0fef54a --- /dev/null +++ "b/\347\277\201\344\277\241\346\240\213/20241223\345\255\246\347\224\237\350\241\250.md" @@ -0,0 +1,251 @@ +## 成绩管理表 +1. 创建模型--`Student.cs` + ```c# + namespace StudentManager.Models; +public class Student +{ + public int Id{get;set;} + public string StudentCode{get;set;}=null!; + public string StudentName{get;set;}=null!; + +} + ``` +2. 创建数据库上下文--`StudentDbContext.cs` + ```c# + using Microsoft.EntityFrameworkCore; + namespace StudentManager.Models; + public class StudentDbContext:DbContext + { + public DbSetStudents{get;set;}=null!; + public DbSetCourses{get;set;}=null!; + public DbSetScores{get;set;}=null!; + + //连接数据库 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var str=$"server=SK-20240829XXWR\\SQLEXPRESS;database=StudentDB;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseSqlServer(str); + } + //DbContext:安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.SqlServer + //1. 生成迁移文件 命令 dotnet ef migrations add XXX + 1.1 需要一个工具 ef工具 安装命令 dotnet tool install --global dotnet-ef + 1.2 安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.Design + 1.3 需要程序不能有编译错误,使用 dotnet build 查看是否有编译错误 + 1.4 程序不能处于运行状态 + //2. 将生成的迁移文件更新到数据库中 命令 dotnet ef database update + } + ``` +3. 视图--`Index.cshtml` + ```c# +
+
+ +
+ 新增 +
+
+
+ + + + + + + + @foreach(var item in @Model) + { + + + + + + + } +
Id学号姓名操作
@item.Id@item.StudentCode@item.StudentName +
+ 编辑 +
+
+ 删除 +
+
+
+ + ``` +4. 增-- `Create.cshtml` + ```c# + @model Student; +
+
+
+ +
+ ``` + +5. 删--`Delete.cshtml` + ```c# + + + + + + + + + + + + + +
姓名:@Model.StudentName
学号:@Model.StudentCode
+ 删除 + + 回到主页 +
+ ``` +6. 改--`Edit.cshtml` + ```c# + @model Student; +
+
+
+ +
+ ``` +7. 查--在Index.cshtml页面中 + ```c# + //查询 + + ``` + + - 数据库 + ```c# + //构造函数 + private readonly StudentDbContext _db; + public ScoresController() + { + _db=new StudentDbContext(); + } + ``` + +1. 增 + ```c# + public IActionResult Create() + { + return View(); + } + /// + /// 新增页面 + /// + /// + /// + [HttpPost] + public IActionResult Create(Student input) + { + //将输入的内容添加的students表 + _db.Students.Add(input); + //保存到数据库 + _db.SaveChanges(); + return RedirectToAction("Index"); + } + /// + /// 获取修改内容 + /// + /// + /// + + ``` +2. 删 + ```c# + /// + /// 删除页面 + /// + /// + /// + public IActionResult Delete(int id) + { + //通过id获取删除的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + return View(list); + } + /// + /// 确认删除页面 + /// + /// + /// + public IActionResult DeleteConfirm(int id) + { + //通过id获取删除的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + //判断list是否为空 + //list不为空 + if(list!=null) + { + _db.Students.Remove(list); + //数据库的内容也需要删除 + _db.SaveChanges(); + //删除完重定向到index页面 + return RedirectToAction("Index"); + } + //不存在则提示错误 + return NotFound(); + } + ``` +3. 改 + ```c# + public IActionResult Edit(int id) + { + //通过点击传入的ID获取要修改此条数据的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + return View(list); + } + /// + /// 修改页面 + /// + /// + /// + [HttpPost] + public IActionResult Edit(Student input) + { + var list =_db.Students.FirstOrDefault(x=>x.Id==input.Id); + //判断list是否存在 + if(list!=null) + { + //修改的内容要在list中重新赋值 + list.StudentCode=input.StudentCode; + list.StudentName=input.StudentName; + //重新传到数据库 + _db.SaveChanges(); + return RedirectToAction("Index"); + } + //不存在则提示错误 + return NotFound(); + } + ``` +4. 查 列表页 + ```c# + public IActionResult Index(string keyword) + { + //先判断keyword是否为空,如果不为空删除前后空格 + keyword=string.IsNullOrEmpty(keyword)?"":keyword.Trim(); + //keyword为空,返回在主页 + if(string.IsNullOrEmpty(keyword)) + { + var list=_db.Students.ToList(); + return View(list); + } + var res=_db.Students.Where(x=>x.StudentName.Contains(keyword)||x.StudentCode.Contains(keyword)).ToList(); + return View(res); + + } + ``` \ No newline at end of file diff --git "a/\347\277\201\344\277\241\346\240\213/20241225\346\210\220\347\273\251\350\241\250.md" "b/\347\277\201\344\277\241\346\240\213/20241225\346\210\220\347\273\251\350\241\250.md" new file mode 100644 index 0000000..0fef54a --- /dev/null +++ "b/\347\277\201\344\277\241\346\240\213/20241225\346\210\220\347\273\251\350\241\250.md" @@ -0,0 +1,251 @@ +## 成绩管理表 +1. 创建模型--`Student.cs` + ```c# + namespace StudentManager.Models; +public class Student +{ + public int Id{get;set;} + public string StudentCode{get;set;}=null!; + public string StudentName{get;set;}=null!; + +} + ``` +2. 创建数据库上下文--`StudentDbContext.cs` + ```c# + using Microsoft.EntityFrameworkCore; + namespace StudentManager.Models; + public class StudentDbContext:DbContext + { + public DbSetStudents{get;set;}=null!; + public DbSetCourses{get;set;}=null!; + public DbSetScores{get;set;}=null!; + + //连接数据库 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var str=$"server=SK-20240829XXWR\\SQLEXPRESS;database=StudentDB;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseSqlServer(str); + } + //DbContext:安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.SqlServer + //1. 生成迁移文件 命令 dotnet ef migrations add XXX + 1.1 需要一个工具 ef工具 安装命令 dotnet tool install --global dotnet-ef + 1.2 安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.Design + 1.3 需要程序不能有编译错误,使用 dotnet build 查看是否有编译错误 + 1.4 程序不能处于运行状态 + //2. 将生成的迁移文件更新到数据库中 命令 dotnet ef database update + } + ``` +3. 视图--`Index.cshtml` + ```c# +
+
+ +
+ 新增 +
+
+
+ + + + + + + + @foreach(var item in @Model) + { + + + + + + + } +
Id学号姓名操作
@item.Id@item.StudentCode@item.StudentName +
+ 编辑 +
+
+ 删除 +
+
+
+ + ``` +4. 增-- `Create.cshtml` + ```c# + @model Student; +
+
+
+ +
+ ``` + +5. 删--`Delete.cshtml` + ```c# + + + + + + + + + + + + + +
姓名:@Model.StudentName
学号:@Model.StudentCode
+ 删除 + + 回到主页 +
+ ``` +6. 改--`Edit.cshtml` + ```c# + @model Student; +
+
+
+ +
+ ``` +7. 查--在Index.cshtml页面中 + ```c# + //查询 + + ``` + + - 数据库 + ```c# + //构造函数 + private readonly StudentDbContext _db; + public ScoresController() + { + _db=new StudentDbContext(); + } + ``` + +1. 增 + ```c# + public IActionResult Create() + { + return View(); + } + /// + /// 新增页面 + /// + /// + /// + [HttpPost] + public IActionResult Create(Student input) + { + //将输入的内容添加的students表 + _db.Students.Add(input); + //保存到数据库 + _db.SaveChanges(); + return RedirectToAction("Index"); + } + /// + /// 获取修改内容 + /// + /// + /// + + ``` +2. 删 + ```c# + /// + /// 删除页面 + /// + /// + /// + public IActionResult Delete(int id) + { + //通过id获取删除的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + return View(list); + } + /// + /// 确认删除页面 + /// + /// + /// + public IActionResult DeleteConfirm(int id) + { + //通过id获取删除的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + //判断list是否为空 + //list不为空 + if(list!=null) + { + _db.Students.Remove(list); + //数据库的内容也需要删除 + _db.SaveChanges(); + //删除完重定向到index页面 + return RedirectToAction("Index"); + } + //不存在则提示错误 + return NotFound(); + } + ``` +3. 改 + ```c# + public IActionResult Edit(int id) + { + //通过点击传入的ID获取要修改此条数据的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + return View(list); + } + /// + /// 修改页面 + /// + /// + /// + [HttpPost] + public IActionResult Edit(Student input) + { + var list =_db.Students.FirstOrDefault(x=>x.Id==input.Id); + //判断list是否存在 + if(list!=null) + { + //修改的内容要在list中重新赋值 + list.StudentCode=input.StudentCode; + list.StudentName=input.StudentName; + //重新传到数据库 + _db.SaveChanges(); + return RedirectToAction("Index"); + } + //不存在则提示错误 + return NotFound(); + } + ``` +4. 查 列表页 + ```c# + public IActionResult Index(string keyword) + { + //先判断keyword是否为空,如果不为空删除前后空格 + keyword=string.IsNullOrEmpty(keyword)?"":keyword.Trim(); + //keyword为空,返回在主页 + if(string.IsNullOrEmpty(keyword)) + { + var list=_db.Students.ToList(); + return View(list); + } + var res=_db.Students.Where(x=>x.StudentName.Contains(keyword)||x.StudentCode.Contains(keyword)).ToList(); + return View(res); + + } + ``` \ No newline at end of file -- Gitee