FreeSql 连接Sqlite和简单的增删改查

发布时间:2026/10/29 19:28:23

FreeSql 连接Sqlite和简单的增删改查 1、开发环境1.1、开发工具VS2022.NET 81.2、GUI框架使用WPF1.3、MVVM使用的是 Prism1.4、WPF控件库使用的是HandyControl1.5、数据库使用的是Sqlite数据库。1.6、Sqlite3安装与数据库创建教程环境设置与基本操作2、项目整体结构如下图。3、主要代码如下。3.1、 App.xaml和App.xaml.cs代码如下。prism:PrismApplication x:ClassFreeSqlDemo.App xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:localclr-namespace:FreeSqlDemo xmlns:hchttps://handyorg.github.io/handycontrol xmlns:prismhttp://prismlibrary.com/ Application.Resources ResourceDictionary ResourceDictionary.MergedDictionaries ResourceDictionary Sourcepack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml/ ResourceDictionary Sourcepack://application:,,,/HandyControl;component/Themes/Theme.xaml/ /ResourceDictionary.MergedDictionaries /ResourceDictionary /Application.Resources /prism:PrismApplicationusing FreeSqlDemo.ViewModels; using FreeSqlDemo.Views; using System.Configuration; using System.Data; using System.Windows; namespace FreeSqlDemo { /// summary /// Interaction logic for App.xaml /// /summary public partial class App : PrismApplication { protected override Window CreateShell() { return Container.ResolveMainWindow(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { } protected override void ConfigureViewModelLocator() { base.ConfigureViewModelLocator(); ViewModelLocationProvider.RegisterMainWindow,MainWindowViewModel(); } } }3.2、MainWindow.xaml代码如下。Window x:ClassFreeSqlDemo.Views.MainWindow xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:localclr-namespace:FreeSqlDemo mc:Ignorabled xmlns:hchttps://handyorg.github.io/handycontrol TitleMainWindow Height450 Width800 StackPanel OrientationVertical Label Content{Binding Info} Margin10 Style{DynamicResource LabelInfo}/ Button Style{StaticResource ButtonPrimary} Content数据库连接测试 Command{Binding InitDbCommand}/ /StackPanel /Window3.3、MainWindowViewModel.cs代码如下。using FreeSqlDemo.DAL; using FreeSqlDemo.Models; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FreeSqlDemo.ViewModels { public class MainWindowViewModel:BindableBase { private string _Info 操作信息; public string Info { get { return _Info; } set { SetProperty(ref _Info, value); } } private DelegateCommand _initDbCommand; public DelegateCommand InitDbCommand { get { return _initDbCommand ?? (_initDbCommand new DelegateCommand(InitDb)); } } private void InitDb() { var blog new Blog { Url http://sample.com }; Info数据库连接状态 DB.Sqlite.Ado.ExecuteConnectTest(); } } }3.4、Blog.cs代码如下。using System; using FreeSql.DataAnnotations; namespace FreeSqlDemo.Models { public class Blog { [Column(IsIdentity true,IsPrimary true)] public int BlogId { get; set; } public string Url { get; set; } public int Rating { get; set; } } }3.5、DB.cs代码如下。using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FreeSqlDemo.DAL { public class DB { static LazyIFreeSql sqliteLazy new LazyIFreeSql(() { var fsql new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, Data SourceDB\DBDemo.db; AttachsDBDemo.db; Poolingtrue;Min Pool Size1;Version3;Journal ModeWAL;Busy Timeout10000;) .UseAutoSyncStructure(true) .Build(); return fsql; }); public static IFreeSql Sqlite sqliteLazy.Value; } }4、运行效果如下图。5、示例代码如下收点积分。https://download.csdn.net/download/xingchengaiwei/901556266、增删改查代只需要替换掉上面例子中的两部分代码即可。6.1、操作界面MainWindow.xaml。Window x:ClassFreeSqlDemo.Views.MainWindow xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:localclr-namespace:FreeSqlDemo mc:Ignorabled xmlns:hchttps://handyorg.github.io/handycontrol TitleMainWindow Height450 Width800 StackPanel OrientationVertical Label Content{Binding Info} Margin10 Style{DynamicResource LabelInfo}/ Button Style{StaticResource ButtonPrimary} Content数据库连接测试 Command{Binding InitDbCommand} Margin10/ Button Style{StaticResource ButtonPrimary} Content添加数据测试 Command{Binding InsertDbCommand} Margin10/ Button Style{StaticResource ButtonPrimary} Content查询数据测试 Command{Binding QueryDbCommand} Margin10/ Button Style{StaticResource ButtonPrimary} Content更新数据测试 Command{Binding UpdateDbCommand} Margin10/ Button Style{StaticResource ButtonPrimary} Content删除数据测试 Command{Binding DeleteDbCommand} Margin10/ /StackPanel /Window6.2、MainWindowViewModel.cs代码如下。using FreeSqlDemo.DAL; using FreeSqlDemo.Models; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FreeSqlDemo.ViewModels { public class MainWindowViewModel : BindableBase { private string _Info 操作信息; public string Info { get { return _Info; } set { SetProperty(ref _Info, value); } } #region 连接数据库测试 private DelegateCommand _initDbCommand; public DelegateCommand InitDbCommand { get { return _initDbCommand ?? (_initDbCommand new DelegateCommand(InitDb)); } } private void InitDb() { Info 数据库连接状态 DB.Sqlite.Ado.ExecuteConnectTest(); } #endregion #region 添加数据 private DelegateCommand _insertDbCommand; public DelegateCommand InsertDbCommand { get { return _insertDbCommand ?? (_insertDbCommand new DelegateCommand(InsertDb)); } } private void InsertDb() { var blog new Blog { Url http://sample.com }; blog.BlogId (int)DB.Sqlite.InsertBlog().AppendData(blog).ExecuteIdentity(); Info 添加数据 BlogId blog.BlogId Url blog.Url Rating blog.Rating; } #endregion #region 查询数据 private DelegateCommand _queryDbCommand; public DelegateCommand QueryDbCommand { get { return _queryDbCommand ?? (_queryDbCommand new DelegateCommand(QueryDB)); } } private void QueryDB() { var blogs DB.Sqlite.SelectBlog() .Where(b b.Url ! string.Empty) .OrderBy(b b.BlogId) .Skip(2) .Limit(10) //第2行-12行的记录 .ToList(); Info blogs.ToArray().ToString(); for (int i 0; i blogs.Count; i) { Debug.WriteLine(blogs[i].BlogId blogs[i].Url blogs[i].Rating \n); } } #endregion #region 更新数据 private DelegateCommand _updateDbCommand; public DelegateCommand UpdateDbCommand { get { return _updateDbCommand ?? (_updateDbCommand new DelegateCommand(UpdateDB)); } } private void UpdateDB() { var blogs DB.Sqlite.UpdateBlog() .Set(b b.Url, http://sample2222.com) .Where(b b.Url http://sample.com) .ExecuteAffrows(); Info 共更新数据 blogs.ToString() 条; } #endregion #region 删除数据 private DelegateCommand _deleteDbCommand; public DelegateCommand DeleteDbCommand { get { return _deleteDbCommand ?? (_deleteDbCommand new DelegateCommand(DeleteDB)); } } private void DeleteDB() { var blogs DB.Sqlite.DeleteBlog() .Where(b b.BlogId 1) .ExecuteAffrows(); Info 共删除数据 blogs.ToString() 条; } #endregion } }
延伸阅读

更多相关文章

2026/10/29 19:28:23

Y20030021 基于PHP+MySQL警务管理系统设计与实现 源代码 配置 文档

警务管理系统1.项目描述2. 开发背景3.项目功能4.界面展示5.源码获取1.项目描述 随着信息技术的迅猛发展,信息化已经成为各行各业发展的必然趋势。公安机关作为国家安全和社会稳定的守护者,其信息化水平直接关系到警务工作的效率和质量。在信息化时代&am…

2026/10/29 19:28:23

华为手机建议使用adb卸载的app

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/29 20:39:02

Java+MySQL疫情物资管理系统设计与实现指南

1. 项目概述:疫情物资管理系统的现实意义 2020年以来的全球公共卫生事件让物资管理系统从边缘课题变成了刚需。我去年指导的毕业设计中,有超过30%的学生选择了相关课题,但普遍存在"重界面轻业务"的问题。这个基于JavaMySQL的物资管…

2026/10/29 20:39:02

API安全测试:REST与GraphQL批量赋值漏洞实战与技能拆解

做 API 安全测试的同行大概都遇到过这种场面:接口文档上明明只写了五个字段,你多塞了一个isAdmin,服务端返回 200,再刷新一次页面,左侧导航栏忽然多出一个后台管理入口。这就是批量赋值漏洞最经典的现场,也…

2026/10/29 20:39:02

基于YOLO的路面裂缝检测系统设计与优化

1. 项目概述:基于深度学习的路面裂缝检测系统这个项目构建了一个完整的端到端路面裂缝检测系统,采用YOLO系列目标检测算法作为核心模型,结合Django框架实现Web可视化界面。系统能够自动识别路面图像中的裂缝缺陷,为道路养护提供智…

2026/10/29 20:39:02

EfficientNetB0迁移学习实现轴承故障小样本诊断

1. 项目概述轴承故障诊断是工业设备预测性维护的核心环节,但传统方法往往需要大量标注样本。这个项目展示了如何利用轻量级网络EfficientNetB0和迁移学习技术,在每类仅需10个训练样本的情况下实现高精度故障分类。我在实际工业场景中验证过这套方案&…

2026/10/29 20:39:02

Tensorboard在AI大模型开发中的核心应用与优化

1. Tensorboard在AI大模型开发中的核心价值 作为深度学习领域最主流的可视化工具,Tensorboard早已成为AI大模型开发者的标配武器。我在多个千万级参数规模的项目中深度使用后,发现它不仅能直观展示模型训练过程,更是调试超参数的利器。特别是…

2026/10/29 20:34:01

达梦数据库与MSSQL跨平台连接配置指南

1. 国产达梦数据库与MSSQLServer跨平台连接方案概述 在金融、政务等关键领域的信息化建设中,常常会遇到国产数据库与国外商业数据库并存的情况。达梦数据库作为国产数据库的代表产品,在实际业务场景中经常需要与Microsoft SQL Server进行数据交互。通过D…

2026/10/29 17:05:52

全国计算机二级C语言真题PDF高效刷题指南

简介:这份PDF是全国计算机二级C语言考试的试题与答案解析,专为备考二级C语言的考生准备。内容涵盖C语言特点与设计理念、程序基本组成单位、main函数执行流程、函数嵌套调用规则、标识符命名规范、整型实型字符型数据类型、八进制十六进制数合法形式、运…

2026/10/29 17:06:02

requests+BeautifulSoup:爬虫抓取h1标题的入门实战指南

简介:一份以实战为导向的Python爬虫入门资源,面向具备基本编程基础、希望快速掌握网络数据抓取的初学者,可解决从零编写简单爬虫的常见困惑。实例以requests库发送HTTP请求、BeautifulSoup解析HTML文档,完整演示从网页中提取 标题…

2026/10/29 17:06:13

递推最小二乘参数辨识MATLAB编程:二阶系统与渐消记忆实现

简介:这是一篇面向控制系统、信号处理与机器学习等领域学习者的最小二乘参数辨识程序说明文档。资源共包含一个docx格式文档,压缩包大小七十八KB,内容围绕递推最小二乘算法和渐消记忆最小二乘算法展开:先构造一个极点位于单位圆内…

2026/10/29 0:03:42

学生选课系统DFD图绘制指南:边界划分与分层校验实战

简介:文档以顶层与第一层DFD图为核心,完整刻画学生选课系统的逻辑模型,面向软件工程课程设计、系统分析学习及毕设前期建模人群。顶层图界定系统边界,外部实体包括管理员、教师和学生三类角色;第一层图进一步细化用户登…

2026/10/29 0:03:42

JIRA敏捷落地:Scrum角色权限、Sprint与燃尽图避坑指南

简介:围绕“基于JIRA的敏捷开发项目管理”这一主题,面向产品负责人、Scrum Master及研发测试成员,梳理Scrum角色分工、迭代流程与JIRA工具配置的对应关系,适合初、中级团队导入敏捷管理时参考。压缩包共1个docx文件,大…

2026/10/29 0:03:42

SEMI S8人机工程验收实战:从条款拆解到现场实测的完整指南

简介:面向半导体设备设计、制造与运维工程师及安全合规人员,这份资料聚焦SEMI标准体系下的设备安全规范,重点收录SEMI S8-0308《半导体制造设备人体工程学工程安全指南》。内容围绕设备设计原则、错误与事故预防、人体工程学适配三大主线展开…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑