From 25c73ce584b6545c8293c0f928354128b370704f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=A4=9A=E9=92=B1?= <3381810463@qq.com> Date: Sun, 22 Dec 2024 17:53:31 +0800 Subject: [PATCH] =?UTF-8?q?20241216=E4=BB=A3=E7=A0=81=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=95=B0=E6=8D=AE=E5=BA=93=E3=80=8120241218C?= =?UTF-8?q?RUD=E5=9F=BA=E7=A1=80=E6=93=8D=E4=BD=9C=E3=80=8120241220CRUD?= =?UTF-8?q?=E8=BF=9B=E9=98=B6=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\346\225\260\346\215\256\345\272\223.md" | 32 +++ ...72\347\241\200\346\223\215\344\275\234.md" | 128 +++++++++++ ...33\351\230\266\346\223\215\344\275\234.md" | 202 ++++++++++++++++++ 3 files changed, 362 insertions(+) create mode 100644 "\350\256\270\350\211\263/20241216\347\254\224\350\256\26013--\344\273\243\347\240\201\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\350\256\270\350\211\263/20241218\347\254\224\350\256\26014--CRUD\345\237\272\347\241\200\346\223\215\344\275\234.md" create mode 100644 "\350\256\270\350\211\263/20241220\347\254\224\350\256\26015--CRUD\350\277\233\351\230\266\346\223\215\344\275\234.md" diff --git "a/\350\256\270\350\211\263/20241216\347\254\224\350\256\26013--\344\273\243\347\240\201\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" "b/\350\256\270\350\211\263/20241216\347\254\224\350\256\26013--\344\273\243\347\240\201\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..0fe7fb0 --- /dev/null +++ "b/\350\256\270\350\211\263/20241216\347\254\224\350\256\26013--\344\273\243\347\240\201\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,32 @@ +## 建立一个数据表 +```cs +public class Student +{ + // 主键id + public int Id{get;set;} + // 学号 + public string StuCore{get;set;}=null!; + // 学生姓名 + public string StuName{get;set;}=null!; +} +``` + +## 在Models内建立一个连接上下文的文件,使用代码连接数据库 +```js +using Microsoft.EntityFrameworkCore; + +public class StuContext : DbContext +{ + public DbSet Students {get;set;}=null!; + + // 调用OnConfiguring方法,是Entity内带的一个方法 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + + var connectionString=$"server=.\\SQLEXPRESS;database=MyStuDb;uid=sa;pwd=123456;TrustServerCertificate=true;"; + + optionsBuilder.UseSqlServer(connectionString); + } +} +``` \ No newline at end of file diff --git "a/\350\256\270\350\211\263/20241218\347\254\224\350\256\26014--CRUD\345\237\272\347\241\200\346\223\215\344\275\234.md" "b/\350\256\270\350\211\263/20241218\347\254\224\350\256\26014--CRUD\345\237\272\347\241\200\346\223\215\344\275\234.md" new file mode 100644 index 0000000..718b209 --- /dev/null +++ "b/\350\256\270\350\211\263/20241218\347\254\224\350\256\26014--CRUD\345\237\272\347\241\200\346\223\215\344\275\234.md" @@ -0,0 +1,128 @@ +## 基础新学的 +1. 一个构造函数 +```js + privite readonly 模型连接名 变量名; + public 控制器名(){ + _db = new 模型连接名(); + } +``` +### 必要条件(一些安装命令) +1. 下载安装包:dotnet add package Microsoft.EntityFrameworkCore.SqlServer +2. 下载安装包:dotnet add package Microsoft.EntityFrameworkCore.Design +3. 下载ef工具:dotnet tool install --global dotnet-ef +4. 生成迁移文件:dotnet ef migrations add Info +5. 将迁移文件更新到数据库:dotnet ef database update +### 代码练习 +2. 在控制器中的代码(返回的是最简单的视图) + +```cs +using Microsoft.AspNetCore.Mvc; +using StuInfo.Models; + +namespace StuInfo.Controllers; + +public class StudentsController:Controller +{ + private readonly StuContext _db; + public StudentsController(){ + _db=new StuContext(); + } + // 查询 + public IActionResult Index(int id) + { + var res=_db.Students.FirstOrDefault(x=>x.Id ==id); + return View(res); + } + public IActionResult Create(int id) + { + var res=_db.Students.FirstOrDefault(x=>x.Id ==id); + return View(res); + } + public IActionResult Edit(int id) + { + var res=_db.Students.FirstOrDefault(x=>x.Id ==id); + return View(res); + } + public IActionResult Delete(int id) + { + var res=_db.Students.FirstOrDefault(x=>x.Id == id); + return View(res); + } +} +``` +3. 数据表中的数据 +```js +namespace StuInfo.Models; + +// 学生信息表 +public class Student +{ + // 主键id + public int Id{get;set;} + // 学号 + public string StuCore{get;set;}=null!; + // 学生姓名 + public string StuName{get;set;}=null!; +} +``` +4. 连接、创建数据库代码 +```cs +namespace StuInfo.Models; + +using Microsoft.EntityFrameworkCore; + +public class StuContext : DbContext +{ + public DbSet Students {get;set;}=null!; + public DbSet Teachers{get;set;}=null!; + + // 调用OnConfiguring方法 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + + var connectionString=$"server=.\\SQLEXPRESS;database=MyStuDb;uid=sa;pwd=123456;TrustServerCertificate=true;"; + + optionsBuilder.UseSqlServer(connectionString); + } +} +``` +5. 视图内的代码 +```html + +@model List; + +@* 查询 *@ +
+
+ + +
+
+
+ 新增 +
+ + + + + + + + + @foreach (var item in Model) + { + + + + + + + } + + +
ID学生学号学生姓名操作
@item.Id@item.StuCore@item.StuName + 修改 + 删除 +
+``` diff --git "a/\350\256\270\350\211\263/20241220\347\254\224\350\256\26015--CRUD\350\277\233\351\230\266\346\223\215\344\275\234.md" "b/\350\256\270\350\211\263/20241220\347\254\224\350\256\26015--CRUD\350\277\233\351\230\266\346\223\215\344\275\234.md" new file mode 100644 index 0000000..f46b50b --- /dev/null +++ "b/\350\256\270\350\211\263/20241220\347\254\224\350\256\26015--CRUD\350\277\233\351\230\266\346\223\215\344\275\234.md" @@ -0,0 +1,202 @@ +## 代码 +1. 控制器中的代码 +```cs +using System.Text.Json; +using Blog.Models; +using Microsoft.AspNetCore.Mvc; + +namespace Blog.Controllers; + +public class BlogsController : Controller +{ + private readonly BlogDbContext _db; + public BlogsController() + { + _db=new BlogDbContext(); + } + // 查询 + public IActionResult Index(string Keyword) + { + // 判断是否为空 + if(string.IsNullOrEmpty(Keyword)) + { + return View(_db.Blogs); + } + else{ + var list =_db.Blogs.Where(x=>x.Title.Contains(Keyword)|| x.Author.Contains(Keyword) || x.Content.Contains(Keyword)).ToList(); + return View(list); + } + + } + // 保留一个之前的 + public IActionResult Create(){ + return View(); + } + // 跳转到新增页面 + [HttpPost] + public IActionResult Create(Blogs input) + { + // 保存新添加的数据 + // 找出最大的ID值,然后在它的基础上加一 + var max =_db.Blogs.Select(t=>t.Id).Max(); + input.Id=max+1; + _db.Blogs.Add(input); + // 返回到首页 + return RedirectToAction("Index"); + } + +// 跳转到编辑页面 + + [HttpPost] + [ValidateAntiForgeryToken] + public IActionResult Edit(Blogs input) + { + if(ModelState.IsValid) + { + // 根据传入的id,从数据库中拿到最新的值 + var blog = _db.Blogs.FirstOrDefault(x=>x.Id == input.Id); + + // 判断是否有对应的id记录 + if(blog !=null){ + blog.Title =input.Title; + blog.Author=input.Author; + blog.Content=input.Content; + } + return RedirectToAction("Index"); + } + return View(input); + } + // 根据id去得到内容然后更改 + public IActionResult Edit(int id) + { + var blog = _db.Blogs.FirstOrDefault(x=>x.Id ==id); + // 返回内容 + return View(blog); + } + // 跳转到删除页面 + // 根据id得到需要删除的内容 + public IActionResult Remove(int id) + { + // 定义一个blog保存查询得到的结果 + var blog =_db.Blogs.FirstOrDefault(x=>x.Id==id); + + // 判断查询到的结果是否为空,为空则提示null,不为空则删除 + if(blog!=null){ + return View(blog); + } + return View(); + } + + // 删除的判断 + public IActionResult Require(int id){ + var blog = _db.Blogs.FirstOrDefault(x=>x.Id==id); + + if(blog!=null){ + // 删除数据 + _db.Blogs.Remove(blog); + } + return RedirectToAction("Index"); + + } +} +``` +2. 视图中的代码 +```html + + @* 模型来源 *@ +@model List + + +
+
+
+ @* *@ + @* *@ + @* 查询 *@ +
+ + +
+
+
+ @* *@ + @* 从官网学的 控制器名称 自建名:即跳转的位置*@ + 新增 +
+
+ + + + + + + + + + @foreach (var blog in @Model) + { + + + + + + + + } + + +
ID标题内容作者操作
@blog.Id@blog.Title@blog.Content@blog.Author + @* + *@ + 编辑 + 删除 +
+
+ + @model Blog.Models; + +
+
+
+
+ +
+ + @model Blog.Models.Blogs; + +
+
+
+
+
+ +
+ + + + @model Blog.Models.Blogs; + +

您将要删除以下内容

+ + + + + + + + + + + + + + + + + +
标题:@Model.Title
内容:@Model.Content
作者:@Model.Author
+ 删除 + + 取消 +
+``` \ No newline at end of file -- Gitee