Blazor + SqlSugar 实现单表增删改功能

2023-12-29 18:36

本文主要是介绍Blazor + SqlSugar 实现单表增删改功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果图

 前台代码

@page "/UserAccount/Index"<Table TItem="BlazorORM.Entity.UserAccount"IsPagination="true" PageItemsSource="new int[] {10, 20}"IsStriped="true" IsBordered="true"ShowToolbar="true" ShowSearch="true" IsMultipleSelect="true" ShowExtendButtons="true"AddModalTitle="增加类型" EditModalTitle="修改类型"SearchModel="@SearchModel" ShowEmpty="true" SearchMode="SearchMode.Top" EditDialogSize="Size.Medium"OnQueryAsync="@OnSearchModelQueryAsync" OnResetSearchAsync="@OnResetSearchAsync"OnAddAsync="@OnAddAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync"><TableColumns><TableColumn @bind-Field="@context.Account" Text="账号" /><TableColumn @bind-Field="@context.Name" Text="名称" />@*<TableColumn @bind-Field="@context.Password" Text="密码" />*@<TableColumn @bind-Field="@context.CreateTime" Width="180" Text="创建时间" Editable="false" /></TableColumns><EditTemplate><div class="row g-3 form-inline"><div class="col-12 col-sm-6"><BootstrapInput @bind-Value="@context.Account" PlaceHolder="请输入" ShowLabel="true" DisplayText="账号" /></div><div class="col-12 col-sm-6"><BootstrapInput @bind-Value="@context.Name" PlaceHolder="请输入" ShowLabel="true" DisplayText="名称" /></div></div><div class="row g-3 form-inline" style="margin-top:2px;"><div class="col-12 col-sm-12"><BootstrapPassword @bind-Value="@context.Password" PlaceHolder="请输入" ShowLabel="true" DisplayText="密码" /></div></div></EditTemplate><SearchTemplate><div class="row g-3 form-inline"><div class="col-12 col-sm-6"><BootstrapInput @bind-Value="@context.Account" PlaceHolder="请输入" ShowLabel="true" DisplayText="账号" /></div><div class="col-12 col-sm-6"><BootstrapInput @bind-Value="@context.Name" PlaceHolder="请输入" ShowLabel="true" DisplayText="名称" /></div></div></SearchTemplate>
</Table><style>.input-group {display: none;}.form-inline .form-label {width: 50px;}
</style>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using BlazorApp;
using BlazorApp.Shared;
using BootstrapBlazor.Components;
using BlazorService;
using System.Diagnostics.CodeAnalysis;namespace BlazorApp.Pages.UserAccount
{public partial class Index{//弹窗组件[Inject][NotNull]private ToastService? ToastService { get; set; }//数据库业务处理对象UserAccountBll bll = new UserAccountBll();//查询条件对象private BlazorORM.Entity.UserAccount SearchModel { get; set; } = new BlazorORM.Entity.UserAccount();/// <summary>/// 清空搜索/// </summary>/// <param name="item"></param>/// <returns></returns>private static Task OnResetSearchAsync(BlazorORM.Entity.UserAccount item){item.Name = "";item.Account = "";return Task.CompletedTask;}/// <summary>/// 查询事件/// </summary>/// <param name="options"></param>/// <returns></returns>private Task<QueryData<BlazorORM.Entity.UserAccount>> OnSearchModelQueryAsync(QueryPageOptions options){int total = 0;var items = bll.GetPageList(SearchModel.Account, SearchModel.Name, options.PageIndex - 1, options.PageItems, total);return Task.FromResult(new QueryData<BlazorORM.Entity.UserAccount>(){Items = items,TotalCount = total,IsSorted = true,IsFiltered = options.Filters.Any(),IsSearch = options.Searches.Any(),IsAdvanceSearch = options.AdvanceSearches.Any()});}/// <summary>/// 添加/// </summary>/// <returns></returns>private static Task<BlazorORM.Entity.UserAccount> OnAddAsync() => Task.FromResult(new BlazorORM.Entity.UserAccount() { CreateTime = DateTime.Now });/// <summary>/// 保存/// </summary>/// <param name="item"></param>/// <param name="changedType"></param>/// <returns></returns>private Task<bool> OnSaveAsync(BlazorORM.Entity.UserAccount item, ItemChangedType changedType){try{bool result = false;// 增加数据演示代码if (changedType == ItemChangedType.Add){item.ID = Guid.NewGuid();if (bll.GetByName(item.Name) != null){ToastService.Show(new ToastOption(){Category = ToastCategory.Error,Title="消息提醒",Content = "名称重复"});return Task.FromResult(false);}if (bll.GetByAcccount(item.Account) != null){ToastService.Show(new ToastOption(){Category = ToastCategory.Error,Title = "消息提醒",Content = "账号重复"});return Task.FromResult(false);}result = bll.Add(item);}else{result = bll.Update(item);}return Task.FromResult(true);}catch (Exception ex){ToastService.Show(new ToastOption(){Category = ToastCategory.Error,Title = "消息提醒",Content = ex.Message});return Task.FromResult(false);}}/// <summary>/// 删除/// </summary>/// <param name="items"></param>/// <returns></returns>private Task<bool> OnDeleteAsync(IEnumerable<BlazorORM.Entity.UserAccount> items){var result = bll.DeleteByIds(items.ToList());return Task.FromResult(result);}}
}

业务处理代码

using BlazorORM;
using BlazorORM.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace BlazorService
{public class UserAccountBll : SqlSugarBase{/// <summary>/// 分页数据/// </summary>/// <param name="account"></param>/// <param name="name"></param>/// <param name="pageIndex"></param>/// <param name="pageSize"></param>/// <param name="totalCount"></param>/// <returns></returns>public List<UserAccount> GetPageList(string account, string name, int pageIndex, int pageSize, int totalCount){return DB.Queryable<UserAccount>().With(SqlWith.NoLock).WhereIF(string.IsNullOrEmpty(account) == false, x => x.Account.Contains(account)).WhereIF(string.IsNullOrEmpty(name) == false, x => x.Name.Contains(name)).OrderBy(x => x.IDQue).ToPageList(pageIndex, pageSize, ref totalCount);}/// <summary>/// 根据名称获取/// </summary>/// <param name="name"></param>/// <returns></returns>public UserAccount GetByName(string name){return DB.Queryable<UserAccount>().With(SqlWith.NoLock).Where(x => x.Name == name).ToList().FirstOrDefault();}/// <summary>/// 根据账号获取/// </summary>/// <param name="account"></param>/// <returns></returns>public UserAccount GetByAcccount(string account){return DB.Queryable<UserAccount>().With(SqlWith.NoLock).Where(x => x.Account == account).ToList().FirstOrDefault();}/// <summary>/// 添加/// </summary>/// <param name="entity"></param>/// <returns></returns>public bool Add(UserAccount entity){return DB.Insertable<UserAccount>(entity).ExecuteCommand() > 0;}/// <summary>/// 修改/// </summary>/// <param name="entity"></param>/// <returns></returns>public bool Update(UserAccount entity){return DB.Updateable<UserAccount>(entity).ExecuteCommand() > 0;}/// <summary>/// 删除/// </summary>/// <param name="entitysList"></param>/// <returns></returns>public bool DeleteByIds(List<UserAccount> entitysList){return DB.Deleteable<UserAccount>(entitysList).ExecuteCommand() > 0;}}
}

这篇关于Blazor + SqlSugar 实现单表增删改功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/550325

相关文章

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

python通过curl实现访问deepseek的API

《python通过curl实现访问deepseek的API》这篇文章主要为大家详细介绍了python如何通过curl实现访问deepseek的API,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编... API申请和充值下面是deepeek的API网站https://platform.deepsee

macOS Sequoia 15.5 发布: 改进邮件和屏幕使用时间功能

《macOSSequoia15.5发布:改进邮件和屏幕使用时间功能》经过常规Beta测试后,新的macOSSequoia15.5现已公开发布,但重要的新功能将被保留到WWDC和... MACOS Sequoia 15.5 正式发布!本次更新为 Mac 用户带来了一系列功能强化、错误修复和安全性提升,进一步增

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

MyBatisX逆向工程的实现示例

《MyBatisX逆向工程的实现示例》本文主要介绍了MyBatisX逆向工程的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录逆向工程准备好数据库、表安装MyBATisX插件项目连接数据库引入依赖pom.XML生成实体类、

C#实现查找并删除PDF中的空白页面

《C#实现查找并删除PDF中的空白页面》PDF文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用Spire.PDFfor.NET通过C#... 目录安装 Spire.PDF for .NETC# 查找并删除 PDF 文档中的空白页C# 添加与删

Java实现MinIO文件上传的加解密操作

《Java实现MinIO文件上传的加解密操作》在云存储场景中,数据安全是核心需求之一,MinIO作为高性能对象存储服务,支持通过客户端加密(CSE)在数据上传前完成加密,下面我们来看看如何通过Java... 目录一、背景与需求二、技术选型与原理1. 加密方案对比2. 核心算法选择三、完整代码实现1. 加密上