以下是一个简单的 .NET Core 6.0 的 MVC 应用程序示例,结合 Layui 和达梦数据库实现基本的增删改查功能:

一、项目创建

(一)创建 .NET Core 6.0 MVC 项目

首先,打开您的开发工具(如 Visual Studio 2022),创建一个新的 .NET Core 6.0 的 MVC 项目。

(二)安装必要的依赖包

在项目中,通过 NuGet 包管理器安装以下依赖包:

  1. DmProvider:用于连接达梦数据库的提供程序。

二、数据库连接与模型

(一)数据库连接配置

在 appsettings.json 文件中添加达梦数据库的连接字符串:

{
  "ConnectionStrings": {
    "DMConnection": "Host=your_host;Port=your_port;User ID=your_user;Password=your_password;Database=your_database"
  }
}

请将 your_hostyour_portyour_useryour_password 和 your_database 替换为您实际的达梦数据库连接信息。


(二)创建数据模型

创建一个名为 Student 的类来表示学生数据:

using System;

namespace YourProject.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

三、数据访问层(DAL)

创建一个 StudentRepository 类来处理与数据库的交互操作:

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)

创建 StudentController 来处理请求和与数据访问层的交互:

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>

六、前端(Layui)

在页面中引入 Layui 的库文件,并使用 Layui 的组件来美化和增强用户界面的交互性。例如,可以使用 Layui 的表格组件来展示学生数据,使用弹出框来进行添加、编辑和删除的确认操作等。