diff --git "a/\347\216\213\351\235\226\350\214\271/20241216-\350\277\236\346\216\245\345\256\236\351\231\205\346\225\260\346\215\256\345\272\223.md" "b/\347\216\213\351\235\226\350\214\271/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/\347\216\213\351\235\226\350\214\271/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/\347\216\213\351\235\226\350\214\271/20241218-CRUD\347\225\214\351\235\242\346\230\276\347\244\272\346\223\215\344\275\234.md" "b/\347\216\213\351\235\226\350\214\271/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/\347\216\213\351\235\226\350\214\271/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/\347\216\213\351\235\226\350\214\271/20241220-\345\242\236\345\210\240\346\224\271.md" "b/\347\216\213\351\235\226\350\214\271/20241220-\345\242\236\345\210\240\346\224\271.md" new file mode 100644 index 0000000000000000000000000000000000000000..0d5e025ba93049734fa327138e21b069d6c3ab90 --- /dev/null +++ "b/\347\216\213\351\235\226\350\214\271/20241220-\345\242\236\345\210\240\346\224\271.md" @@ -0,0 +1,75 @@ +# 新增 +```cs +@model Blog.Models.Blogs; +
+
+
+
+ +
+``` +```cs +public IActionResult Create() + { + + return View(); + } + [HttpPost] + public IActionResult Create(Blogs input) + { + var maxId=Db.Blogs.Select(t=>t.Id).Max(); + input.Id=maxId+1; + Db.Blogs.Add(input); + return RedirectToAction("Index"); + } + +``` +# 编辑 +```cs +public IActionResult Edit(int id) +{ + // 获取id + var Blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + return View(Blog); +} +[HttpPost] +public IActionResult Edit(Blogs input) +{ + // 获取Id + var blog = Db.Blogs.FirstOrDefault(x => x.Id == input.Id); + + if (blog != null) + { + blog.Title = input.Title; + blog.Content = input.Content; + blog.Author = input.Author; + } + return RedirectToAction("Index"); +} + +``` +# 删除 +```cs +public ActionResult Delete(int id) +{ + var product = db.Products.Find(id); + if (product == null) + { + return HttpNotFound(); + } + return View(product); +} +[HttpPost, ActionName("Delete")] +[ValidateAntiForgeryToken] +public ActionResult DeleteConfirmed(int id) +{ + var product = db.Products.Find(id); + if (product == null) + { + return HttpNotFound(); + } + db.Products.Remove(product); + db.SaveChanges(); + return RedirectToAction("Index"); +} +``` \ No newline at end of file