WPF用户登录界面设计-使用SQLite数据库进行存储

一、SQLite数据库介绍

SQLite是一款轻量级的关系型数据库,它小巧高效,无需服务器配置,仅需单一文件即可存储数据。SQLite跨平台支持,易于集成到各种应用程序中,并支持SQL语言进行数据操作。它保证了数据的完整性、一致性和持久性,通过文件级锁定实现并发访问的安全。SQLite广泛应用于移动、桌面和嵌入式系统,是管理本地数据的理想选择。其长期稳定性和开发团队的支持承诺,使其成为数字内容存储的可靠选择。

二、效果演示:

三、项目结构:

1.项目结构

2.数据库内容

四、例子代码

C#项目中安装System.Data.SQLite。这可以通过NuGet包管理器来完成。在Visual Studio中,你可以通过“管理NuGet包”来搜索并安装System.Data.SQLite

1.前端Xaml文件
<Grid ><Grid.RowDefinitions><RowDefinition Height="120"/><RowDefinition Height="50"/><RowDefinition Height="50"/><RowDefinition Height="50"/><RowDefinition Height="auto"/></Grid.RowDefinitions><Button Content="数据创建"   HorizontalAlignment="Left"  Click="DataAddition"  Height="30" Margin="30,0,0,0"/><StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock Text="用户名:"  Margin="0,5,0,0"/><TextBox x:Name="txtUsername" Width="200" Height="30" Margin="0,0,15,0"/></StackPanel><StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock Text="密  码:"  Margin="0,5,0,0"/><PasswordBox x:Name="txtPassword" Width="200" Height="30" Margin="0,0,15,0"/></StackPanel><StackPanel Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center"><Button Content="登录"  Background="#0047AB" Foreground="White"  HorizontalAlignment="Right"  Click="LoginButton_Click" Width="200" Height="30" Margin="30,0,0,0"/></StackPanel><TextBlock Grid.Row="4" x:Name="myTextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14"/></Grid>
2.后端Cs文件
    public partial class MainWindow : Window{string dbConnectionString = "Data Source=D:\\Quality managemen program\\UpperComputer\\列子程序\\tryWpfApp3-sq用户登录\\WpfApp1\\date\\mydatabase.db; Version=3;";public MainWindow(){InitializeComponent();using (var connection = new SQLiteConnection(dbConnectionString)){connection.Open();// 创建表(如果尚不存在)  CreateTable(connection);}}private void DataAddition(object sender, RoutedEventArgs e){using (var connection = new SQLiteConnection(dbConnectionString)){connection.Open();for (int i = 0; i < 10; i++){// 插入数据  InsertData(connection);}                   }}private void LoginButton_Click(object sender, RoutedEventArgs e){string input_name = txtUsername.Text;string input_password = txtPassword.Password;using (var connection = new SQLiteConnection(dbConnectionString)){connection.Open();string querySql = "SELECT * FROM Users";using (var command = new SQLiteCommand(querySql, connection))using (var reader = command.ExecuteReader()){int UserPermissions = 0;while (reader.Read()){int id = reader.GetInt32(0);string name = reader.GetString(1);string password = reader.GetString(2);int permissions = reader.GetInt32(3);Debug.WriteLine($"ID: {id}, Name: {name}, PassWord: {password}, Permissions: {permissions}");if (name == input_name && password == input_password){UserPermissions = permissions;}}Debug.WriteLine($"当前权限为{UserPermissions}");myTextBlock.Text = $"当前权限为{UserPermissions}";}}}/*//*/// 创建表static void CreateTable(SQLiteConnection connection){string createTableSql = "CREATE TABLE IF NOT EXISTS Users (" +"Id INTEGER PRIMARY KEY AUTOINCREMENT, " +"Name TEXT NOT NULL, " +"PassWord TEXT, " +"Permissions INT)";using (var command = new SQLiteCommand(createTableSql, connection)){command.ExecuteNonQuery();}}// 插入数据 static void InsertData(SQLiteConnection connection){string insertSql = "INSERT INTO Users (Name, PassWord, Permissions) VALUES (@name, @password, @permissions)";using (var command = new SQLiteCommand(insertSql, connection)){command.Parameters.AddWithValue("@name", "John Doe");command.Parameters.AddWithValue("@password", "123456");command.Parameters.AddWithValue("@permissions", 1);command.ExecuteNonQuery();// 可以插入多条数据,只需重复上述过程即可  }}// 查询数据  static void ReadData(SQLiteConnection connection){string querySql = "SELECT * FROM Users";using (var command = new SQLiteCommand(querySql, connection))using (var reader = command.ExecuteReader()){int UserPermissions = 0;while (reader.Read()){int id = reader.GetInt32(0);string name = reader.GetString(1);string password = reader.GetString(2);int permissions = reader.GetInt32(3);Debug.WriteLine($"ID: {id}, Name: {name}, PassWord: {password}, Permissions: {permissions}");if (name == "AA" && password == "999999"){UserPermissions = permissions;}}Debug.WriteLine($"当前权限为{UserPermissions}");}}// 更新数据  static void UpdateData(SQLiteConnection connection){string updateSql = "UPDATE Users SET PassWord = @newPassWord WHERE Id = @id";using (var command = new SQLiteCommand(updateSql, connection)){command.Parameters.AddWithValue("@newPassWord", "8888");command.Parameters.AddWithValue("@id", 3);command.ExecuteNonQuery();}}}

五、C#控制台数据库读写方法例子

using System;
using System.Data.SQLite; // 引入SQLite命名空间  class Program
{static void Main(string[] args){// 数据库连接字符串  string dbConnectionString = "Data Source=D:\\Quality managemen program\\UpperComputer\\C#K\\读写SQlite数据库\\ConsoleApp1\\data\\mydatabase.db; Version=3;";// 创建并连接到SQLite数据库  using (var connection = new SQLiteConnection(dbConnectionString)){connection.Open();// 创建表(如果尚不存在)  CreateTable(connection);for (int i = 0; i < 10; i++){// 插入数据  InsertData(connection);}// 查询数据  ReadData(connection);更新数据  //UpdateData(connection);再次查询数据以查看更新  //ReadData(connection);}Console.WriteLine("Press any key to exit...");Console.ReadKey();}// 创建表static void CreateTable(SQLiteConnection connection){string createTableSql = "CREATE TABLE IF NOT EXISTS Users (" +"Id INTEGER PRIMARY KEY AUTOINCREMENT, " +"Name TEXT NOT NULL, " +"PassWord TEXT, " +"Permissions INT)";using (var command = new SQLiteCommand(createTableSql, connection)){command.ExecuteNonQuery();}}// 插入数据 static void InsertData(SQLiteConnection connection){string insertSql = "INSERT INTO Users (Name, PassWord, Permissions) VALUES (@name, @password, @permissions)";using (var command = new SQLiteCommand(insertSql, connection)){command.Parameters.AddWithValue("@name", "John Doe");command.Parameters.AddWithValue("@password", "123456");command.Parameters.AddWithValue("@permissions", 1);command.ExecuteNonQuery();// 可以插入多条数据,只需重复上述过程即可  }}// 查询数据  static void ReadData(SQLiteConnection connection){string querySql = "SELECT * FROM Users";using (var command = new SQLiteCommand(querySql, connection))using (var reader = command.ExecuteReader()){int UserPermissions = 0;while (reader.Read()){int id = reader.GetInt32(0);string name = reader.GetString(1);string password = reader.GetString(2);int permissions = reader.GetInt32(3);Console.WriteLine($"ID: {id}, Name: {name}, PassWord: {password}, Permissions: {permissions}");if (name =="AA" && password=="999999"){UserPermissions = permissions;}}Console.WriteLine($"当前权限为{UserPermissions}");}}// 更新数据  static void UpdateData(SQLiteConnection connection){string updateSql = "UPDATE Users SET PassWord = @newPassWord WHERE Id = @id";using (var command = new SQLiteCommand(updateSql, connection)){command.Parameters.AddWithValue("@newPassWord", "8888");command.Parameters.AddWithValue("@id", 3);  command.ExecuteNonQuery();}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3281265.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

计算机网络03

文章目录 重传机制超时重传快速重传SACK 方法Duplicate SACK 滑动窗口流量控制操作系统缓冲区与滑动窗口的关系窗口关闭糊涂窗口综合症 拥塞控制慢启动拥塞避免算法拥塞发生快速恢复 如何理解是 TCP 面向字节流协议&#xff1f;如何理解字节流&#xff1f;如何解决粘包&#xf…

免费【2024】springboot 滁州市特产销售系统

博主介绍&#xff1a;✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌ 技术范围&#xff1a;SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化…

vue2学习 -- 核心语法

文章目录 前置简介1. 模板语法2. 数据2.1 数据绑定2.2 el与data的两种写法2.3 MVVM模型2.4 Object.defineProperty2.5 Vue中的数据代理 3. 事件3.1 事件处理3.2 事件修饰符3.3 键盘事件 4. 计算属性5. 监视(侦听)属性5.1 书写形式5.2 深度监视5.3 简写形式5.4 计算属性和监听属…

大数据-53 Kafka 基本架构核心概念 Producer Consumer Broker Topic Partition Offset 基础概念了解

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; 目前已经更新到了&#xff1a; Hadoop&#xff08;已更完&#xff09;HDFS&#xff08;已更完&#xff09;MapReduce&#xff08;已更完&am…

美团2024年春招第一场笔试[测开方向],编程题+选择题详解,ACM式C++解法

编程题&选择题 编程题小美的平衡矩阵思路代码 小美的数组询问思路代码 验证工号思路代码 选择题1.在计算机网络中&#xff0c;端口号的作用是什么2.HTTPS协议通过使用哪些机制来确保通信的安全性3.Etag用于标识资源的唯一标识符&#xff0c;他可以用于4.在一个单道系统中&a…

Nacos配置到springboot快速入门(笔记)

本人学习中的简单笔记&#xff0c;本文写的极其不详细&#xff0c;慎看&#xff01;&#xff01;&#xff01; Nacos 简介 Nacos 致力于帮助开发者发现、配置和管理微服务。它提供了一组简单易用的特性集&#xff0c;帮助开发者快速实现动态服务发现、服务配置、服务元数据及…

CSCP、CPIM和CLMP三大证书的区别?如何选择?

在制造型企业、供应链和运营管理专业人士都会不断寻找方法来提升他们的技能和职业前景。三种流行的认证——CSCP&#xff08;Certified Supply Chain Professional&#xff09;、CPIM&#xff08;Certified in Planning and Inventory Management&#xff09;以及CLMP&#xff…

创客项目秀 | 基于xiao的光剑

在《星球大战》宇宙中&#xff0c;光剑不仅仅是武器;它们是持有者与原力的桥梁&#xff0c;制造一把光剑几乎是每个创客的梦想&#xff0c;今天给大家带来的是国外大学生团队制作的可伸缩光剑项目。 材料清单&#xff1a; 电机驱动模块1:90减速电机套装MP3模块、喇叭Xiao RP2…

一「骑」就LUCKY!凯迪拉氪强劲动力,带你一路顺畅,幸运随行!

好运&#xff0c;其实就是毫不费劲的完成心里所想的事情。简单来说&#xff0c;是不需要太多努力&#xff0c;就能得到比较大的回报。每个人都希望自己拥有好运气&#xff0c;但这就跟抽盲盒一样&#xff0c;可能穷极一生都享受不到。 所以&#xff0c;与其期待虚无缥缈的好运…

爬虫问题---ChromeDriver的安装和使用

一、安装 1.查看chrome的版本 在浏览器里面输入 chrome://version/ 回车查看浏览器版本 Chrome的版本要和ChromeDriver的版本对应&#xff0c;否则会出现版本问题。 2.ChromeDriver的版本选择 114之前的版本&#xff1a;https://chromedriver.storage.googleapis.com/index.ht…

生成式AI在金融领域的研究与应用

引言 科技的进步日新月异&#xff0c;伴随着移动终端与网络的不断迭代&#xff0c;人工智能领域也从专家系统到卷积神经网络&#xff0c;从Transform到生成式AI&#xff0c;乃至未来的AGI&#xff0c;不断改变我们的生活方式&#xff0c; 科技发展驱动人类社会加速迈向全面智能…

无人机之交通管理篇

无人机技术已经渗透到社会的各个领域&#xff0c;其中交通监控与管理便是其应用的重要方向之一。无人机凭借其独特的优势&#xff0c;如高效性、灵活性、实时性等&#xff0c;为交通监控与管理带来了革命性的变革。 一、无人机在交通监控中的应用 1、实时监控与数据采集 无人…

SuperMap iDesktopXiClient3D for WebGL 基于确定性空间插值生成水体流场

目录 摘要1 原始数据解析2 数据空间插值2.1流场UVW0.dat文件转xlsx2.2生成流场点数据2.3生成U、V栅格数据2.4裁剪U、V栅格数据2.5生成零值棋盘网格2.6生成U、V棋盘栅格 3 棋盘栅格转棋盘点3.1U、V棋盘栅格矢量化3.2U、V字段追加3.3流场数据JSON标准解析3.3.1流场数据JSON范例3.…

数据结构与算法-索引堆及其优化

&#x1f49d;&#x1f49d;&#x1f49d;首先&#xff0c;欢迎各位来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里不仅可以有所收获&#xff0c;同时也能感受到一份轻松欢乐的氛围&#xff0c;祝你生活愉快&#xff01; 文章目录 引言一、索引堆…

基于SpringBoot+Vue的档案管理系统(带1w+文档)

基于SpringBootVue的档案管理系统(带1w文档) 基于SpringBootVue的档案管理系统(带1w文档) 随着信息化的不断发展&#xff0c;科技的进步也越来越大。软件编程是一个不断发展的行业&#xff0c;每个行业都必须进行适合自身特点的系统开发&#xff0c;才能在机构中生存和发展。当…

区块链技术在智能城市中的创新应用探索

随着全球城市化进程的加速和信息技术的快速发展&#xff0c;智能城市成为了未来城市发展的重要方向。在智能城市建设中&#xff0c;区块链技术作为一种去中心化、安全和透明的分布式账本技术&#xff0c;正逐渐展现出其在优化城市管理、提升公共服务和增强城市安全性方面的潜力…

【大模型系列篇】Vanna-ai基于检索增强(RAG)的sql生成框架

简介 Vanna是基于检索增强(RAG)的sql生成框架 Vanna 使用一种称为 LLM&#xff08;大型语言模型&#xff09;的生成式人工智能。简而言之&#xff0c;这些模型是在大量数据&#xff08;包括一堆在线可用的 SQL 查询&#xff09;上进行训练的&#xff0c;并通过预测响应提示中最…

JVM: 堆上的数据存储

文章目录 一、对象在堆中的内存布局1、对象在堆中的内存布局 - 标记字段2、JOL打印内存布局 二、元数据指针 一、对象在堆中的内存布局 对象在堆中的内存布局&#xff0c;指的是对象在堆中存放时的各个组成部分&#xff0c;主要分为以下几个部分&#xff1a; 1、对象在堆中的…

真没想到BitLocker加密的系统碰上CrowdStrike蓝屏故障也能恢复如初!

网管小贾 / sysadm.cc 三声金钟响&#xff0c;六阵御鼓催。 百官列两扉&#xff0c;天子临朝威。 是日早朝&#xff0c;八宝金殿之中&#xff0c;天子端坐龙椅上。 群臣山呼&#xff1a;“万岁&#xff0c;万岁&#xff0c;万万岁……&#xff01;” 天子面沉似水、不怒自威…

【数学建模】【优化算法】:【MATLAB】从【一维搜索】到】非线性方程】求解的综合解析

目录 第一章&#xff1a;一维搜索问题 黄金分割法 股票交易策略优化 总结&#xff1a; 第二章&#xff1a;线性规划 线性规划&#xff08;Simplex 算法&#xff09; 生产计划优化 总结&#xff1a; 第三章&#xff1a;无约束非线性优化问题 梯度下降法 神经网络训练…