diff --git "a/\345\220\264\344\275\263\351\242\226/2024.12.16\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.md" "b/\345\220\264\344\275\263\351\242\226/2024.12.16\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..b3041a4e90ba879234cffa768c4a21e74adda151 --- /dev/null +++ "b/\345\220\264\344\275\263\351\242\226/2024.12.16\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.md" @@ -0,0 +1,219 @@ +# MVC 数据库连接 +```cs +// 在Program.cs文件中 +var connectionString=$"Server=.\\SQLEXPRESS;Database=MdBlog;uid=sa;pwd=123456;TrustServerCertificate=true;"; + +builder.Services.AddDbContext(opt=> +{ + opt.UseSqlServer(connectionString); +}); + +builder.Services.AddScoped(); +在Models文件中创建一个DbContext.cs文件 +public class BlogDbContext:DbContext{ + public BlogDbContext(DbContextOptions options) : base(options) + { + } + public DbSetBlog{get;set;}=null!; +} +// 在终端:------ +dotnet add package Microsoft.EntityFrameworkCore.Sqlserver---》安装 +dotnet tool install --global dotnet-ef---》 +dotnet add package Microsoft.EntityFrameworkCore.Design----》安装 +dotnet ef migrations add InitCreate----》迁移 +dotnet ef database update ---》更新 +``` +## 1. 项目初始化 + +首先,创建一个新的 MVC 项目。 +## 2. 添加必要的包和库 + +为了在 MVC 项目中连接数据库,通常需要使用 **Entity Framework (EF)**。可以通过 **NuGet 包管理器** 安装: + +1. 右键点击项目名称,选择 **管理 NuGet 程序包**。 +2. 在 **浏览** 选项卡中,搜索 **Entity Framework**。 +3. 选择 **Entity Framework** 包,点击 **安装**。 +4. 等待安装完成后,关闭包管理器窗口。 + +或者,使用 **Package Manager Console** 运行以下命令: + +```powershell +Install-Package EntityFramework +``` + +## 3. 配置数据库连接字符串 + +数据库连接字符串定义了应用程序如何连接到数据库。在 **Web.config** 文件中进行配置: + +1. 打开 **Web.config** 文件。 +2. 在 `` 节点下找到 `` 节点。如果不存在,可以手动添加。 +3. 添加连接字符串。例如,连接到 **SQL Server** 数据库: + +```xml + + + +``` + +## 4. 创建数据模型(Model) + +数据模型代表数据库中的表结构。在 **Models** 文件夹中创建一个新的模型类: + +1. 右键点击 **Models** 文件夹,选择 **添加** > **类**。 +2. 命名,例如 **Product.cs**,然后点击 **添加**。 +3. 定义模型属性。例如: + + +## 5. 创建数据库上下文(DbContext) + +**DbContext** 是 EF 提供的核心类,用于与数据库进行交互。在 **Models** 文件夹中创建一个新的上下文类: + +1. 右键点击 **Models** 文件夹,选择 **添加** > **类**。 +2. 命名,例如 **MyDbContext.cs**,然后点击 **添加**。 +3. 定义上下文类: + +```csharp +using System.Data.Entity; + +public class MyDbContext : DbContext +{ + // 构造函数,传递连接字符串名称 + public MyDbContext() : base("name=MyDbContext") + { + } + + // 定义 DbSet 属性,代表数据库中的表 + public DbSet Products { get; set; } +} +``` + +**注意**:`name=MyDbContext` 对应于 **Web.config** 中的连接字符串名称。 + +## 6. 配置数据库迁移 + +数据库迁移允许通过代码管理数据库架构的变化。以下是启用迁移的步骤: + +1. 打开 **Package Manager Console**。 +2. 选择 **默认项目** 为包含 **DbContext** 的项目。 +3. 运行以下命令以启用迁移: + +```powershell +Enable-Migrations +``` + +4. 创建初始迁移: + +```powershell +Add-Migration InitialCreate +``` + +5. 更新数据库: + +```powershell +Update-Database +``` + +**注意**:如果尚未创建数据库,**Update-Database** 将根据模型创建数据库。如果数据库已存在且包含数据,请谨慎操作。 + +## 7. 实现控制器(Controller) + +控制器负责处理用户请求,并与模型和视图进行交互。以下是创建 **ProductsController** 控制器的步骤: + +1. 右键点击 **Controllers** 文件夹,选择 **添加** > **控制器**。 +2. 选择 **MVC 5 控制器 - 空**,点击 **添加**。 +3. 命名,例如 **ProductsController**,点击 **添加**。 +4. 在控制器中定义动作方法。例如: + +```csharp +using System.Web.Mvc; +using MvcDatabaseConnection.Models; + +public class ProductsController : Controller +{ + private MyDbContext db = new MyDbContext(); + + // 显示产品列表 + public ActionResult Index() + { + var products = db.Products.ToList(); + return View(products); + } + + // 显示创建产品表单 + public ActionResult Create() + { + return View(); + } + + // 处理创建产品请求 + [HttpPost] + public ActionResult Create(Product product) + { + if (ModelState.IsValid) + { + db.Products.Add(product); + db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(product); + } + + // 显示编辑产品表单 + public ActionResult Edit(int id) + { + var product = db.Products.Find(id); + if (product == null) + { + return HttpNotFound(); + } + return View(product); + } + + // 处理编辑产品请求 + [HttpPost] + public ActionResult Edit(Product product) + { + if (ModelState.IsValid) + { + db.Entry(product).State = EntityState.Modified; + db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(product); + } + + // 删除产品 + public ActionResult Delete(int id) + { + var product = db.Products.Find(id); + if (product == null) + { + return HttpNotFound(); + } + db.Products.Remove(product); + db.SaveChanges(); + return RedirectToAction("Index"); + } + + // 释放资源 + protected override void Dispose(bool disposing) + { + if (disposing) + { + db.Dispose(); + } + base.Dispose(disposing); + } +} +``` + +## 8. 创建视图(View) + +视图负责呈现数据。以下是创建 **Index** 视图的步骤: + +1. 在 **Views** 文件夹下找到 **Products** 文件夹。如果没有,手动创建。 +2. 右键点击 **Products** 文件夹,选择 **添加** > **视图**。 +3. 选择 **List** 模板,点击 **添加**。 +4. 在 **Index.cshtml** 中,编辑视图以显示产品列表 \ No newline at end of file diff --git "a/\345\220\264\344\275\263\351\242\226/2024.12.18\350\277\201\347\247\273\346\226\207\344\273\266.md" "b/\345\220\264\344\275\263\351\242\226/2024.12.18\350\277\201\347\247\273\346\226\207\344\273\266.md" new file mode 100644 index 0000000000000000000000000000000000000000..877744c1bdc63dd783e34a931151f161c6ae03eb --- /dev/null +++ "b/\345\220\264\344\275\263\351\242\226/2024.12.18\350\277\201\347\247\273\346\226\207\344\273\266.md" @@ -0,0 +1,10 @@ +1. 生成迁移文件,命令是:`dotnet er migrations add FirstInit` + +**前提条件:** +- 需要一个Design的依赖包,安装命令是:`dotnet add package Microsoft.EntityFrameworkCore.Design` +- 需要一个工具包,这个工具是`dotnet -ef`,全局安装的命令是:`dotnet tool install --global donet-ef` +- 程序不能有编译错误,使用`dotnet build`命令来排查编译错误 +- 程序不能处于运行状态 + + +2. 将上一步生成的迁移文件同步到数据库中,命令是:`dotnet er database update` \ No newline at end of file diff --git "a/\345\220\264\344\275\263\351\242\226/2024.12.20\346\225\260\346\215\256\345\272\223\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\345\220\264\344\275\263\351\242\226/2024.12.20\346\225\260\346\215\256\345\272\223\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..67897db73c94860693c8b7878817ee3b72ec83c5 --- /dev/null +++ "b/\345\220\264\344\275\263\351\242\226/2024.12.20\346\225\260\346\215\256\345\272\223\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,107 @@ +# 数据库增删改查 +## 新增 +```cs +@model Baby.Models.Babys; +
+
+
+
+ +
+``` +```cs +public IActionResult Create() + { + + return View(); + } + [HttpPost] + public IActionResult Create(Babys input) + { + var maxId=Db.Babys.Select(t=>t.Id).Max(); + input.Id=maxId+1; + Db.Babys.Add(input); + return RedirectToAction("Index"); + } + +``` +## 编辑 +```cs +public IActionResult Edit(int id) +{ + // 获取id + var baby = Db.Babys.FirstOrDefault(x => x.Id == id); + return View(baby); +} +[HttpPost] +public IActionResult Edit(Babys input) +{ + // 获取Id + var blog = Db.Babys.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"); +} +``` +## 查 +```cs +using System.Linq; +using System.Web.Mvc; +using MvcDatabaseConnection.Models; + +public class ProductsController : Controller +{ + private MyDbContext db = new MyDbContext(); + + // 显示产品列表 + public ActionResult Index() + { + var products = db.Products.ToList(); + return View(products); + } + + // 显示单个产品详情 + public ActionResult Details(int id) + { + var product = db.Products.Find(id); + if (product == null) + { + return HttpNotFound(); + } + return View(product); + } + + // 其他动作方法(Create, Edit, Delete)... +} +``` \ No newline at end of file diff --git "a/\345\220\264\344\275\263\351\242\226/2024.12.23\344\274\252\346\225\260\347\273\204.md" "b/\345\220\264\344\275\263\351\242\226/2024.12.23\344\274\252\346\225\260\347\273\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..e31951d7f2e645f39399f2bf22ff2d3684a82733 --- /dev/null +++ "b/\345\220\264\344\275\263\351\242\226/2024.12.23\344\274\252\346\225\260\347\273\204.md" @@ -0,0 +1,30 @@ +### MVC中的伪数组概念 + +在ASP.NET Core MVC框架中,伪数组通常指的是那些提供数组式访问但具有更高级功能的集合类型。这些集合在处理动态数据和响应式Web开发中非常有用。 + +#### 数组(Array) + +- 数组是一种基本的数据结构,用于存储固定大小的同类型元素集合。 +- 数组的大小在声明时确定,并且之后不能改变。 +- 数组通过索引访问元素,索引从0开始。 + +#### List(伪数组) + +- `List`是C#中最常用的伪数组实现,尤其在ASP.NET Core MVC中处理动态数据时非常有用。 +- 它提供了与数组类似的索引访问方式,但`List`的大小是动态的,可以根据需要增加或减少元素。 +- `List`还提供了许多方便的方法,如`Add`、`Remove`、`Insert`等,用于操作集合中的元素,这在处理模型数据时非常有用。 + +#### Dictionary(键值对集合) + +- `Dictionary`或`SortedDictionary`提供了键值对的存储方式,这在ASP.NET Core MVC中处理路由和查询参数时非常有用。 +- 它们允许快速检索和存储与特定键关联的值,这对于构建动态和响应式的Web应用至关重要。 + +#### 应用场景 + +在ASP.NET Core MVC中,伪数组和集合的使用非常广泛: + +- **动态数据管理**:`List`可以用来管理用户提交的表单数据,动态添加或删除项目。 +- **路由和查询参数**:`Dictionary`可以用来存储和检索路由参数和查询字符串参数。 +- **响应式Web开发**:在构建响应式Web应用时,这些集合类型可以帮助开发者更灵活地处理数据和用户交互。 + +通过理解这些伪数组和集合的工作原理,ASP.NET Core MVC开发者可以更有效地管理数据和构建动态Web应用。 \ No newline at end of file diff --git "a/\345\220\264\344\275\263\351\242\226/2024.12.25\345\255\246\347\224\237\350\241\250.md" "b/\345\220\264\344\275\263\351\242\226/2024.12.25\345\255\246\347\224\237\350\241\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..16c4845ebfa1265381e1a963a4f1399781c7cc80 --- /dev/null +++ "b/\345\220\264\344\275\263\351\242\226/2024.12.25\345\255\246\347\224\237\350\241\250.md" @@ -0,0 +1,44 @@ +### 伪删除的实现方式:在数据库中添加一个标记字段,用于表示记录是否被“删除”。这个字段通常是一个整数类型,比如flag或DeletionStateCode,其中某个值(如0)表示记录是活跃的,而另一个值(如1)表示记录已被“删除” +。 + +### 代码示例:在ASP.NET MVC框架中,伪删除可以通过修改实体的状态码来实现。例如,通过检查Not参数,如果Not不为null,则将用户的状态码设置为1,表示用户已被“删除” +1. 定义显示页面的方法 + + public IActionResult Index(string su){ + //检查该字符串是否为空,如果是则返回没有被删除的列表视图 + if(string.IsNullOrEmpty(su)){ + return View(_db.Course.Where(x=>x.IsDeleted!=true)); + } + //浏筛选没有被删除的信息 + var res=_db.Course.Where(x=>!x.IsDeleted); + //从中筛选包含su的信息 + var list=res.Where(x=>x.Id.ToString().Contains(su)||x.CourseName.Contains(su)).ToList(); + return View(list); + } + + 或 + public IActionResult Index(string su){ + IEnumerablelist; + if(string.IsNullOrEmpty(su)){ + return View(_db.Course); + }else{ + //浏筛选没有被删除的信息 + var res=_db.Course.Where(x=>!x.IsDeleted); + //从中筛选包含su的信息 + var list=res.Where(x=>x.Id.ToString().Contains(su)||x.CourseName.Contains(su)).ToList(); + } + return View(list); + + } +2. 创建一个私有的静态方法,接受一个Course对象和一个字符串keyword作为参数,并返回一个布尔值。 + + private static bool Judge(Course stu,string keyword){ + //改属性如果为true,则返回false,及表示被删除的信息不显示 + if(stu.IsDeleted){ + return false; + } + if(stu.Id.ToString().Contains(keyword)||stu.CourseName.Contains(keyword)){ + return true; + } + return false; + } \ No newline at end of file