.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();
        }
    }
}
相关推荐
程序员柳32 分钟前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
梦在深巷、1 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT乌鸦坐飞机1 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
IT_10241 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
祁思妙想2 小时前
八股学习(三)---MySQL
数据库·学习·mysql
惊骇世俗王某人3 小时前
1.MySQL之如何定位慢查询
数据库·mysql
秦歌6663 小时前
向量数据库-Milvus快速入门
数据库·milvus
Edingbrugh.南空4 小时前
Flink SQLServer CDC 环境配置与验证
数据库·sqlserver·flink
码不停蹄的玄黓5 小时前
MySQL分布式ID冲突详解:场景、原因与解决方案
数据库·分布式·mysql·id冲突
爱上语文5 小时前
Redis基础(6):SpringDataRedis
数据库·redis·后端