.net8 blazor auto模式很爽(五)读取sqlite并显示(1)

为了访问sqlite,我们需要在Nuget中安装Microsoftr的EntityFrameworkCore、EntityFrameworkCore.Sqlite、EntityFrameworkCore.Sqlite.Core

在SharedLibrary的Models里增加employee

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharedLibrary.Models
{
    
        public class employee
        {
            public string? 人员姓名 { get; set; }
            public string? 证件号码 { get; set; }
            public string? 单位名称 { get; set; }
            public string? 性别 { get; set; }
        }
    
}

我们在Client的Pages里面增加一个Testtable.razor,里面放一个table用来显示sqlite里面一张表的内容。

cs 复制代码
@page "/testtable"
@rendermode InteractiveAuto

<PageTitle>测试sqlite取数</PageTitle>

    <button class="btn btn-primary" @onclick="getemployees">获取数据</button>
    <table class="table">
        <thead>
            <tr>
                <th>人员姓名</th>
                <th>证件号码</th>
                <th>单位名称</th>
                <th>性别</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in employees)
            {
                <tr>
                    <td>@forecast.人员姓名</td>
                    <td>@forecast.证件号码</td>
                    <td>@forecast.单位名称</td>
                    <td>@forecast.性别</td>
                </tr>
            }
        </tbody>
    </table>


@code {
    private List<employee> employees=new List<employee>();
    
    protected override async Task   OnInitializedAsync()
    {
    }
    private async Task getemployees()
    {
        employees = await EmployeeService.GetEmployeesAsync();
        StateHasChanged(); // 通知 Blazor 组件数据已更新
    }
    


}

在BlazorApp1的Controllers里增加EmployeeController:

cs 复制代码
using Microsoft.AspNetCore.Mvc;
using SharedLibrary.Models;
using BlazorApp1.data;
using Microsoft.EntityFrameworkCore;

namespace BlazorApp1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController
    {
        employee employeels = new employee();
        private readonly dbcont _context;

        public EmployeeController(dbcont context)
        {
            _context = context;
        }
        [HttpGet("Getemployee")]
        public async Task<ActionResult<IEnumerable<employee>>> Getemployee()
        {
            return await _context.employee.Take(10).ToListAsync();
        }
    }
}
相关推荐
玉成2264 分钟前
MySQL两表之间数据迁移由于字段排序规则设置的不一样导致失败
数据库·mysql
dblens 数据库管理和开发工具13 分钟前
DBLens:让 SQL 查询更智能、更高效的数据库利器
服务器·数据库·sql·数据库连接工具·dblens
TDengine (老段)24 分钟前
TDengine 在新能源领域的最佳实践
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
是席木木啊27 分钟前
Spring Boot 中 @Async 与 @Transactional 结合使用全解析:避坑指南
数据库·spring boot·oracle
__风__27 分钟前
PostgreSQL 创建扩展后台流程
数据库·postgresql
StarRocks_labs27 分钟前
Fresha 的实时分析进化:从 Postgres 和 Snowflake 走向 StarRocks
数据库·starrocks·postgres·snowflake·fresha
CodeAmaz33 分钟前
Spring编程式事务详解
java·数据库·spring
scan72438 分钟前
python mcp 打印出参数
linux·服务器·数据库
Evan芙41 分钟前
mysql二进制部署以及多实例部署
android·数据库·mysql
Access开发易登软件1 小时前
Access开发实战:绘制漏斗图实现业务转化分析
数据库·信息可视化·html·vba·图表·access