以下是一个简单的 .NET Core 6.0
的 MVC
应用程序示例,结合 Layui
和达梦数据库实现基本的增删改查功能:
一、项目创建
(一)创建 .NET Core 6.0
MVC
项目
(二)安装必要的依赖包
二、数据库连接与模型
(一)数据库连接配置
{
"ConnectionStrings": {
"DMConnection": "Host=your_host;Port=your_port;User ID=your_user;Password=your_password;Database=your_database"
}
}
请将 your_host
、your_port
、your_user
、your_password
和 your_database
替换为您实际的达梦数据库连接信息。
(二)创建数据模型
using System;
namespace YourProject.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
三、数据访问层(DAL)
using Dapper;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using YourProject.Models;
namespace YourProject.Data
{
public class StudentRepository
{
private readonly IDbConnection _connection;
public StudentRepository(IDbConnection connection)
{
_connection = connection;
}
public async Task<IEnumerable<Student>> GetAllStudents()
{
return await _connection.QueryAsync<Student>("SELECT * FROM Students");
}
public async Task<Student> GetStudentById(int id)
{
return await _connection.QueryFirstOrDefaultAsync<Student>("SELECT * FROM Students WHERE Id = @Id", new { Id = id });
}
public async Task<int> AddStudent(Student student)
{
return await _connection.ExecuteAsync("INSERT INTO Students (Name, Age) VALUES (@Name, @Age)", student);
}
public async Task<int> UpdateStudent(Student student)
{
return await _connection.ExecuteAsync("UPDATE Students SET Name = @Name, Age = @Age WHERE Id = @Id", student);
}
public async Task<int> DeleteStudent(int id)
{
return await _connection.ExecuteAsync("DELETE FROM Students WHERE Id = @Id", new { Id = id });
}
}
}
四、控制器(Controller)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using YourProject.Data;
using YourProject.Models;
namespace YourProject.Controllers
{
public class StudentController : Controller
{
private readonly StudentRepository _studentRepository;
public StudentController(StudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
public async Task<IActionResult> Index()
{
var students = await _studentRepository.GetAllStudents();
return View(students);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(Student student)
{
await _studentRepository.AddStudent(student);
return RedirectToAction("Index");
}
public async Task<IActionResult> Edit(int id)
{
var student = await _studentRepository.GetStudentById(id);
return View(student);
}
[HttpPost]
public async Task<IActionResult> Edit(Student student)
{
await _studentRepository.UpdateStudent(student);
return RedirectToAction("Index");
}
public async Task<IActionResult> Delete(int id)
{
await _studentRepository.DeleteStudent(id);
return RedirectToAction("Index");
}
}
}
五、视图(View)
(一)Index.cshtml
@{
ViewData["Title"] = "学生列表";
}
<h2>学生列表</h2>
<table class="table">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach (var student in Model)
{
<tr>
<td>@student.Id</td>
<td>@student.Name</td>
<td>@student.Age</td>
<td>
<a href="/Student/Edit/@student.Id" class="btn btn-primary">编辑</a>
<a href="/Student/Delete/@student.Id" class="btn btn-danger">删除</a>
</td>
</tr>
}
</tbody>
</table>
<a href="/Student/Create" class="btn btn-success">添加学生</a>
(二)Create.cshtml
@{
ViewData["Title"] = "添加学生";
}
<h2>添加学生</h2>
<form asp-action="Create" method="post">
<div class="form-group">
<label for="Name">姓名</label>
<input type="text" class="form-control" id="Name" name="Name" />
</div>
<div class="form-group">
<label for="Age">年龄</label>
<input type="number" class="form-control" id="Age" name="Age" />
</div>
<button type="submit" class="btn btn-primary">添加</button>
</form>
(三)Edit.cshtml
@{
ViewData["Title"] = "编辑学生";
}
<h2>编辑学生</h2>
<form asp-action="Edit" method="post">
<input type="hidden" name="Id" value="@Model.Id" />
<div class="form-group">
<label for="Name">姓名</label>
<input type="text" class="form-control" id="Name" name="Name" value="@Model.Name" />
</div>
<div class="form-group">
<label for="Age">年龄</label>
<input type="number" class="form-control" id="Age" name="Age" value="@Model.Age" />
</div>
<button type="submit" class="btn btn-primary">保存</button>
</form>