diff --git "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-16 \346\225\260\346\215\256\345\272\223\344\274\240\345\205\245.md" "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-16 \346\225\260\346\215\256\345\272\223\344\274\240\345\205\245.md" index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e344e3b76217bfcdbf70845efe1e6050a171d7dd 100644 --- "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-16 \346\225\260\346\215\256\345\272\223\344\274\240\345\205\245.md" +++ "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-16 \346\225\260\346\215\256\345\272\223\344\274\240\345\205\245.md" @@ -0,0 +1,32 @@ +应用EntityFrameworkCore的步骤 +安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer +定义数据库表模型 +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!; +} + +定义数据库上下文 +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=.\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} + +生成迁移文件,命令:dotnet ef migrations add XXX (PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +将上一步生成的迁移文件,更新到数据库:dotnet ef database update(PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程 \ No newline at end of file diff --git "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-18\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-18\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..73145ff212f28d556ed8e53bc832252c81883e4c 100644 --- "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-18\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" +++ "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-18\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,177 @@ +创建一个mvc项目 命令 `dotnet new mvc -o StudentManager` + +2. 模型 Student.cs + ```c# + namespace StudentManager.Models; + public class Student + { + public int Id{get;set;} //主键Id + public string StudentCode{get;set;}=null!; //学号 + public string StudentName{get;set;}=null!; //姓名 + } + ``` + +3. 数据库上下文 StudentDbContext.cs + ```c# + using Microsoft.EntityFrameworkCore; + namespace StudentManager.Models; + public class StudentDbContext:DbContext + { + public DbSetStudents{get;set;}=null!; + + //连接数据库 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuidler) + { + var str=$"server=SK-20240829LVRN\\SQLEXPRESS;database=StudentDb;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuidler.UseSqlServer(str); + } + } + + + //DbContext:安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.SqlServer + //1. 生成迁移文件 命令 dotnet ef migrations add XXX + 1.1 需要一个工具 ef工具 安装命令 dotnet tool install --global dotnet-ef + 1.2 安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.Design + 1.3 需要程序不能有编译错误,使用 dotnet build 查看是否有编译错误 + 1.4 程序不能处于运行状态 + //2. 将生成的迁移文件更新到数据库中 命令 dotnet ef database update + ``` + +4. 控制器 StudentsController.cs + ```c# + using Microsoft.AspNetCore.Mvc; + using StudentManager.Models; + namespace StudentManager.Controllers; + public class StudentsController:Controller + { + //引用数据库 + private readonly StudentDbContext _db; + public StudentsController() + { + _db=new StudentDbContext(); + } + + public IActionResult Index() + { + return View(); + } + } + ``` +5. 创建对应的视图 Students--Index.cshtml + ```c# + @model List; + +
+
+ +
+ 新增 +
+
+
+ + + + + + + + + @foreach(var item in @Model) + { + + + + + + + + } +
Id姓名性别年龄操作
@item.Id@item.Name@item.Sex@item.Age +
+ 编辑 +
+
+ 删除 +
+
+
+
+ ``` +6. css样式 + ```c# + body{ + display: flex; + justify-content: center; + } + .box + { + width: 350px; + } + .top + { + width: 350px; + display: flex; + margin-top: 5px; + } + .on{ + width: 350px; + } + table,tr,td,th + { + border: 1px solid black; + border-collapse: collapse; + } + table + { + width: 350px; + text-align: center; + } + th{ + background-color: rgb(255, 250, 201); + } + .cz + { + border: none; + display: flex; + justify-content: space-around; + } + a{ + text-decoration: none; + color: white; + } + .add + { + width: 40px; + height: 25px; + text-align: center; + line-height: 25px; + border-radius: 5px; + background-color: rgb(144, 247, 118); + position: relative; + left: 60px; + } + .edit + { + width: 40px; + height: 25px; + text-align: center; + line-height: 25px; + border-radius: 5px; + background-color: rgb(188, 222, 253); + } + .delete + { + width: 40px; + height: 25px; + text-align: center; + line-height: 25px; + border-radius: 5px; + background-color: rgb(241, 171, 226); + } + ``` \ No newline at end of file diff --git "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-19.md" "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-19.md" index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..da222e2c938dcc39b628367b88395f949ae232ae 100644 --- "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-19.md" +++ "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-19.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/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-23 \347\254\224\350\256\260.md" "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-23 \347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..6ec4ce0b94715552eaa4ae6f0f657d15280da527 --- /dev/null +++ "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-23 \347\254\224\350\256\260.md" @@ -0,0 +1,11 @@ +使用迁移创建数据库 + +dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +ef工具 dotnet tool install --global dotnet-ef + +安装依赖包 dotnet add package Microsoft.EntityFrameworkCore.Design + +生成迁移文件 dotnet ef migrations add xxx + +将迁移文件同步到数据库 dotnet ef database update \ No newline at end of file diff --git "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-25 \347\254\224\350\256\260.md" "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-25 \347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..3337dacfeacc21a6d76afcb771cbb46ce8b62d24 --- /dev/null +++ "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-25 \347\254\224\350\256\260.md" @@ -0,0 +1,22 @@ +``` +if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['query'])) { + $query = $_GET['query']; + $results = Model::search($query); + render('search_results', ['results' => $results, 'query' => $query]); +} +``` +``` +Models +class Model { + public static function search($query) { + + return Database::query("SELECT * FROM items WHERE name LIKE ?", ['%' . $query . '%']); + } +} +``` +``` +
+ + +
+``` \ No newline at end of file diff --git "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-26 \347\254\224\350\256\260.md" "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-26 \347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..ecaf7fc5ddc34ce2a67c94f1ac22c3573aa669a7 --- /dev/null +++ "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-26 \347\254\224\350\256\260.md" @@ -0,0 +1,28 @@ +``` +public IActionResult Index(string keyword) + { + var list = _db.Scores.ToList(); + var stus = _db.Students.Where(x=>!x.IsDeleted).ToList(); + var cours = _db.Courses.Where(x=>!x.IsDeleted).ToList(); + var res = list.Select(x => { + var tmpStu = stus.FirstOrDefault(y=>y.Id==x.StudentId); + var tmpCourse = cours.FirstOrDefault(z=>z.Id==x.CourseId); + return new{ + x.Id, + x.StudentId, + StudentName = tmpStu == null ? "" : tmpStu.StudentName, + x.CourseId, + CourseName = tmpCourse == null ? "默认课程" : tmpCourse.CourseName, + x.Score + }; + }); + return View(res); + // keyword = string.IsNullOrEmpty(keyword) ? " ": keyword.Trim(); + // if (string.IsNullOrEmpty(keyword)) + // { + // return View(_db.Scores.ToList()); + // } + // keyword = keyword.Trim(); + // var list = _db.Scores.Where( x => x.StudentId.ToString().Contains(keyword)).ToList(); + } +``` \ No newline at end of file