From 67594c8702f21cbceb895c2108ed542c4f0ed43b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=A9=89=E5=A9=B7?= <14091842+dreaming-of-becoming-a-fupo@user.noreply.gitee.com> Date: Mon, 23 Dec 2024 02:30:01 +0800 Subject: [PATCH] =?UTF-8?q?12.19=E4=BD=9C=E4=B8=9A=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\346\225\260\346\215\256\345\272\223.md" | 58 ++++++++ ...36\346\225\260\346\215\256\345\272\223.md" | 37 +++++ ...36\345\210\240\346\224\271\346\237\245.md" | 131 ++++++++++++++++++ 3 files changed, 226 insertions(+) create mode 100644 "\345\274\240\345\251\211\345\251\267/20241216-\345\256\214\346\225\264\344\274\240\345\205\245\345\210\260\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\345\274\240\345\251\211\345\251\267/20241218-\347\234\237\345\256\236\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\345\274\240\345\251\211\345\251\267/20241219-\345\242\236\345\210\240\346\224\271\346\237\245.md" diff --git "a/\345\274\240\345\251\211\345\251\267/20241216-\345\256\214\346\225\264\344\274\240\345\205\245\345\210\260\346\225\260\346\215\256\345\272\223.md" "b/\345\274\240\345\251\211\345\251\267/20241216-\345\256\214\346\225\264\344\274\240\345\205\245\345\210\260\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..da222e2 --- /dev/null +++ "b/\345\274\240\345\251\211\345\251\267/20241216-\345\256\214\346\225\264\344\274\240\345\205\245\345\210\260\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,58 @@ +# 可将数据传输到数据库保存 +## 1.安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +## 2.定义数据库表模型 +- 在Models中的Blogs.cs中 +```c# +namespace Blog.Models; + +public class Blogs +{ + public int Id { get; set; } + public string Title { get; set; } = null!; + public string Content { get; set; } = null!; + public string Author { get; set; } = null!; +} +``` +## 3. 定义数据库上下文 +- 在Models中创建 BlogDbContext.cs文件 +```c# +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set; } = null!; + + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=SK-20240829DIVS\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} +``` + +## 4. 生成迁移文件,命令:dotnet ef migrations add XXX (PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +``` +dotnet tool install -- global dotnet-ef +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet ef migration add InitialCreate +``` + +- 到这里要先在文件Blog.csproj中出现 +```c# + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + +``` +## 5.将上一步生成的迁移文件,更新到数据库:dotnet ef database update +`dotnet ef database update` +(PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) + +- 结束后,在文件夹中会出现Migrations文件,就可以在数据库中看到你保存的数据 \ No newline at end of file diff --git "a/\345\274\240\345\251\211\345\251\267/20241218-\347\234\237\345\256\236\346\225\260\346\215\256\345\272\223.md" "b/\345\274\240\345\251\211\345\251\267/20241218-\347\234\237\345\256\236\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..17bb00a --- /dev/null +++ "b/\345\274\240\345\251\211\345\251\267/20241218-\347\234\237\345\256\236\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,37 @@ +# 数据库应用 + ```C# + var list =students.Select(stu=> + { + //查找当前学生Id对应的课程,只选择课程名称 + var temCourse=Course.Where(x=>x.StudentId=stu.Id).Select(x=>x.CourseName); + //将一个字符串集合变成一个以逗号间隔的字符串 + var str=string.Join(",",list); + //重新组合学生信息和课程字符串信息,准备返回 + var res=new{stu.Id,stu.Name,stu.Age,CourseName=str}; + return res; + } + //过滤下没有选修课程的记录 + var newlist = list.Where(x=>x.CourseName.Length>0); + //打印学生信息 + foreach (var item in newlist){ + cw($"{item.Id}-{item.Name}-{item.Age}-{item.CourseName}") + } + + ) + ``` +### 真实数据库 + + ```C# + //readonly 只读 + private readonly BlogContext _db; + + //构造函数 + + public BlogContext() + { + _db=new BlogContext(); + } + + //保存到数据库 + _db.SaveChanges(); + ``` \ No newline at end of file diff --git "a/\345\274\240\345\251\211\345\251\267/20241219-\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\345\274\240\345\251\211\345\251\267/20241219-\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000..3d3cb5e --- /dev/null +++ "b/\345\274\240\345\251\211\345\251\267/20241219-\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,131 @@ +# 学生管理系统 +## 1. 在Controller中 +- StudentController.cs +```cs +using Microsoft.AspNetCore.Mvc; +using StudentManger3.Models; + +namespace StudentManger3.Controllers; + +public class StudentsController : Controller +{ + + private readonly StudentDbContext _db; + + public StudentsController() + { + _db = new StudentDbContext(); + } + public IActionResult Index() + { + // 从数据库传数据到Index里面 + var list = _db.Students.ToList(); + return View(list); + } + + public IActionResult Delete() + { + return View(); + } + + public IActionResult Edit() + { + return View(); + } + public IActionResult Create() + { + return View(); + } +} +``` +## 2. Views +- Student => Index.cshtml +```html +@model List; +
+ @* form是查询的,新增用a标签 *@ +
+
+ 新增 + + + + + + + + + @foreach(var item in @Model) + { + + + + + + + } +
编号姓名年龄操作
@item.Id@item.StudentName@item.Age + 编辑 + 删除 +
+
+ +``` + +- Delete.cshtml +```html + + + + + + + + + + + + + +
姓名:@Model.StudentName
年龄@Model.Age
+ 删除 + + 回到首页 +
+``` \ No newline at end of file -- Gitee