尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
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 } }
RELATED

相关推荐

Y20030015基于Java+springboot+mysql基于微信小程序的课堂笔记系统的设计与实现 源码 论文

Y20030015基于Java+springboot+mysql基于微信小程序的课堂笔记系统的设计与实现 源码 论文

课堂笔记小程序1.摘 要2. 背景和意义3.系统功能4.界面展示5.源码获取1.摘 要 随着计算机技术的成熟,互联网的建立,如今,PC平台上有许多关于云笔记方面的应用程序,但由于使用时间和地点上的限制,用户在使用上存在着种种…

📅 2026/10/29 19:28:19
Y20030021 基于PHP+MySQL警务管理系统设计与实现 源代码 配置 文档

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

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

📅 2026/10/29 19:28:19
华为手机建议使用adb卸载的app

华为手机建议使用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 19:28:19
MORE NEWS

更多资讯

📰

AI测试为何不能照搬传统测试:概率正确性、蜕变测试与概念漂移

简介:ISTQB认证测试工程师人工智能测试中文大纲(CT-AI-1.0-CN-1.0)面向希望系统掌握AI系统质量保障与测试方法的测试工程师、质量人员及备考认证者,围绕人工智能、深度学习测试与模型测试建立从概念到实践的知识框架。资源为1个PD…

📰

物流管理系统套打技术:精准打印与效率提升方案

1. 物流管理系统套打功能的核心价值在物流仓储行业,单据管理一直是运营环节中的重要痛点。传统物流企业每天需要处理大量出入库单、货运单、配送单等纸质单据,这些单据往往采用预先印刷好的格式。佳易王物流管理系统创新的套打功能,完美解决了…

📰

计算机网络性能监控与优化实战指南

1. 计算机网络基础概念解析作为网络工程师的入门必修课,理解计算机网络的基础架构就像建筑工人必须熟悉砖瓦水泥的特性。计算机网络本质上是由通信线路连接的设备集合,这些设备遵循特定协议进行数据交换。现代网络采用分层设计思想,最经典的O…

📰

物流单据套打技术:精准打印与动态模板匹配解析

1. 项目概述:物流单据套打的业务痛点与价值 在物流仓储行业,单据管理一直是运营环节中最基础却最容易出错的环节。我们经常看到这样的场景:仓库管理员手忙脚乱地翻找各种单据,司机在装货区焦急等待提货单,财务人员因为…

📰

SuperScan 4.0 老系统兼容性实战:从安装到端口扫描的完整指南

简介:这份资源是面向网络安全初学者与运维人员的 SuperScan 扫描工具中文使用教程,以 Word 文档形式系统讲解这款 Windows 平台端口扫描与主机信息收集工具的操作方法,帮助读者快速上手并理解各功能模块的用途。压缩包内共 1 个 doc 文件&…

📰

Java实现自动登录:HTTP请求构造与Cookie管理实战

简介:这份资源面向需要实现人人网(原校内网)自动登录功能的Java开发者,针对网上流传的旧版校内登录代码无法直接使用的问题,提供了一套可运行的简易登录实现。作者基于Java编写核心登录逻辑,并配套打包了所…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬