diff --git "a/\345\210\230\347\201\277/20241216-\350\277\236\346\216\245\345\256\236\351\231\205\346\225\260\346\215\256\345\272\223.md" "b/\345\210\230\347\201\277/20241216-\350\277\236\346\216\245\345\256\236\351\231\205\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000000000000000000000000000000000000..efd609fb570983667743fc36f8c4b7b4762b172b --- /dev/null +++ "b/\345\210\230\347\201\277/20241216-\350\277\236\346\216\245\345\256\236\351\231\205\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,70 @@ +## 连接实际数据库 + +### 数据库上下文初始化 +#### 使用[有参数]的方案 +1. 在Program.cs文件中添加连接数据库字符串 +```cs +var connectionString = $"Server=.;Database=MdBlog;uid=sa;pwd=123456;TrustServerCertificate=true"; + +builder.Services.AddDbContext(opt => +{ + opt.UseSqlServer(connectionString); +}); +builder.Services.AddScoped(); +``` +2. 在对应的Models文件中,不需要重写OnConfiguring的代码,只需要集成base的代码 +```cs +public BlogDbContext(DbContextOptions options) : base(options) +{ +} +``` + +#### 使用[无参数]的方案!!! +1. 在Program.cs文件中不需要填写任何的代码 +2. 在对应的Models文件中,需要有重写OnConfiguring的代码,继承base的代码则不再需要 +3. 将连接数据库的字符串代码写入重写OnConfiguring的代码中 +```cs +protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) +{ + base.OnConfiguring(optionsBuilder); + + var connectionString = $"Server=.;Database=MdBlog;uid=sa;pwd=123456;TrustServerCertificate=true"; + optionsBuilder.UseSqlServer(connectionString); +} +``` + +### 连接数据库 +1. 在控制器代码中实例化数据库对象 +```cs +private readonly BlogDbContext _db; +public BlogsController() +{ + _db = new BlogDbContext(); +} +``` +2. 将原本的模拟数据库替换成实际数据库 +``` +Db.Blogs --> _db.Blogs +``` + - 如果改完之后出现`An unhandled exception occurred while processing the request.`的报错,则在主页面表示显示的代码后加上**ToList()** + ```cs + return View(_db.Blogs.ToList()); + ``` +3. 在每次修改(增删改)操作结束之后,都要进行保存,将内容保存到数据库 +```cs +_db.SaveChanges(); +``` + +## 打开sql快捷方式 +win+R services.msc + +## join方法 +是字符串方法,用于将一个数组或集合中的元素连接成一个单一的字符串 + +语法:string.Join("连接符", 字符串数组/集合/对象数组) + +```cs +List fruits = new List { "Apple", "Banana", "Cherry" }; +string result = String.Join(", ", fruits); +Console.WriteLine(result); // 输出: Apple, Banana, Cherry +``` \ No newline at end of file diff --git "a/\345\210\230\347\201\277/20241218-CRUD\347\225\214\351\235\242\346\230\276\347\244\272\346\223\215\344\275\234.md" "b/\345\210\230\347\201\277/20241218-CRUD\347\225\214\351\235\242\346\230\276\347\244\272\346\223\215\344\275\234.md" new file mode 100644 index 0000000000000000000000000000000000000000..d10bc1223de0440ba73876e699daa06bbccae26a --- /dev/null +++ "b/\345\210\230\347\201\277/20241218-CRUD\347\225\214\351\235\242\346\230\276\347\244\272\346\223\215\344\275\234.md" @@ -0,0 +1,130 @@ +## 连接实际数据库 +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 + 修改 + 删除 +
+``` \ No newline at end of file diff --git "a/\345\210\230\347\201\277/20241220-\345\242\236\345\210\240\346\224\271\346\237\245\344\273\243\347\240\201.md" "b/\345\210\230\347\201\277/20241220-\345\242\236\345\210\240\346\224\271\346\237\245\344\273\243\347\240\201.md" new file mode 100644 index 0000000000000000000000000000000000000000..f46b50b41714babed27cec1e19706f651b2478ed --- /dev/null +++ "b/\345\210\230\347\201\277/20241220-\345\242\236\345\210\240\346\224\271\346\237\245\344\273\243\347\240\201.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