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

news2024/9/9 6:42:06

一、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://www.coloradmin.cn/o/1962644.html

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

相关文章

Java数据结构和算法中文版(第2版)详细教程

前言 数据结构是指数据在计算机存储空间中(或磁盘中)的安排方式。算法是指软件程序用来操作这些结构中的数据的过程。几乎所有的计算机程序都使用数据结构和算法&#xff0c;即使最简单的程序也不例外。比如设想一个打印地址标签的程序&#xff0c;这个程序使用一个数组来存储…

整理几个常用的Linux命令(Centos发行版)

如果工作中需要经常整理一些文档&#xff0c;需要汇总一下&#xff0c;现有的服务器资源信息&#xff0c;那么这篇文章适合你&#xff1b; 如果你是一名开发者&#xff0c;需要经常登录服务器&#xff0c;排查应用的出现的一些问题&#xff0c;那么这篇文章适合你&#xff1b;…

使用java判断字符串中是否包含中文汉字

1.导入huool工具的maven依赖 <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.16</version></dependency>2.复制一下代码直接运行 import cn.hutool.core.lang.Validator;public …

面向对象 - 概述、类的创建、 实例化与内存解析

一、学习面向对象的三条主线 Java类及类的成员&#xff1a;&#xff08;重点&#xff09;属性、方法、构造器&#xff1b;&#xff08;熟悉&#xff09;代码块、内部类面向对象的特征&#xff1a;封装、继承、多态、&#xff08;抽象&#xff09;其他关键字的使用&#xff1a;…

机械学习—零基础学习日志(高数16——函数极限性质)

零基础为了学人工智能&#xff0c;真的开始复习高数 这里我们继续学习函数极限的性质。 局部有界性 充分条件与必要条件 极限存在是函数局部有界的充分条件。什么是充分条件&#xff0c;什么是必要条件呢&#xff1f;我这里做了一点小思考&#xff0c;和大家分享&#xff0c…

alibaba cloud linux+JDK+TOMCAT+NGINX+PHP+MYSQL配置实践

CentOs要停止维护了&#xff0c;一直在服务器上用的CentOs7也最迟到2024年6月了&#xff0c;这次给公司新购一台备用服务器&#xff0c;在选择操作系统的时候&#xff0c;考虑了一下&#xff0c;决定试用一下阿里云的alibaba cloud linux。 alibaba cloud linux分为2和3版本&am…

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

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

ingress使用HostNetwork部署

1.三种常用的部署模式 1.1 DeploymentLoadBalancer模式的service 用Deployment部署igress-controller&#xff0c;创建一个type为LoadBalancer的service关联这组pod。大部分公有云&#xff0c;都会为LoadBalancer的service自动创建一个负载均衡器&#xff0c;通常还绑定了公网…

Java面试八股之Spring如何解决循环依赖

Spring如何解决循环依赖 在Spring框架中&#xff0c;循环依赖问题通常发生在两个或多个Bean相互依赖的情况下。Spring为了解决循环依赖问题&#xff0c;采用了不同的策略&#xff0c;这些策略主要取决于Bean的作用域以及依赖注入的方式。下面是一些关键点&#xff1a; 单例Be…

护眼灯真的有用吗?护眼灯到底该不该买?

护眼灯真的有用吗&#xff1f;随着科技的发展&#xff0c;生活质量水平的不断提升&#xff0c;大家对于生活的要求也在不断拔高。护眼台灯进入众多家庭里面&#xff0c;成为不可或缺的产品。然而&#xff0c;护眼台灯在市面上&#xff0c;种类颇多&#xff0c;其质量也是参差不…

力扣高频SQL 50题(基础版)第三十三题

文章目录 力扣高频SQL 50题&#xff08;基础版&#xff09;第三十三题610.判断三角形题目说明实现过程准备数据实现方式结果截图 力扣高频SQL 50题&#xff08;基础版&#xff09;第三十三题 610.判断三角形 题目说明 表: Triangle ----------------- | Column Name | Typ…

Python入门宝藏《看漫画学Python》,495页漫画带你弄清python知识点!简单易懂 | 附PDF全彩版

华为出品的《看漫画学Python》全彩PDF教程是一本适合Python初学者的学习资料&#xff0c;通过漫画的形式将复杂的Python技术问题简单化&#xff0c;使学习过程更加生动有趣。以下是对该教程的内容简介、本书概要及本书目录的详细解析&#xff1a; 内容简介 《看漫画学Python》…

无线领夹麦哪个品牌音质最好?无线领夹麦克风怎么挑选

在直播行业中&#xff0c;声音质量直接影响着观众的观看体验。一款优质的无线领夹麦克风&#xff0c;能够确保你的声音在直播过程中始终保持清晰、稳定&#xff0c;减少背景噪音的干扰。它不仅方便佩戴&#xff0c;还能让你在移动中自由发挥&#xff0c;无需担心线缆束缚。对于…

数说故事 | 社媒聆听“顶流”红山动物园UGC声量

7月&#xff0c;CASETiFY和南京红山森林动物园联名啦&#xff0c;一个号称“手机壳中的爱马仕”&#xff0c;一个是“动物园顶流”&#xff0c;两大IP梦幻联动&#xff0c;推出了“明星动物”系列手机壳&#xff0c;CASETiFY还解锁“饲养员”身份&#xff0c;认养了酷酷的美洲豹…

某土地市场网JS逆向:debugger脚本限制秒退和webpack hash参数加密

&#x1f50d;某土地市场网逆向思路 &#x1f6ab; 解决网页反debugger &#x1f50d; 网页禁止打开开发者工具 在访问中国土地市场网时&#xff0c;我们会发现网页禁止了开发者工具的使用&#xff0c;包括F12和右键调试。 &#x1f50d;强制进入开发者工具 窗口关闭并回退 …

IDEA对线上项目远程debug

1、在启动脚本上添加以下配置内容 -agentlib:jdwptransportdt_socket,servery,suspendn,address*:5005 nohup java -agentlib:jdwptransportdt_socket,servery,suspendn,address5005 -jar test.jar > misc.out & 2、在IDEA中进行配置 &#xff08;1&#xff09;选择远程…

我们的网站被狗爬了!

大家好&#xff0c;我是程序员鱼皮。 世风日下&#xff0c;人心不古。我们的程序员面试刷题网站 《面试鸭》 才刚刚上线了一个多月&#xff0c;就由于过于火爆&#xff0c;被不少同行和小人发起网络攻击。 而且因为我们已经有 4500 多道人工整理的企业高频面试题、100 多个各…

不得不安利的程序员开发神器,太赞了!!

作为一名程序员&#xff0c;你是否常常为繁琐的后端服务而感到头疼&#xff1f;是否希望有一种工具可以帮你简化开发流程&#xff0c;让你专注于创意和功能开发&#xff1f;今天&#xff0c;我要向大家隆重推荐一款绝佳的开发神器——MemFire Cloud。它专为懒人开发者准备&…

【前端】(仅思路)如何在前端实现一个fc手柄,将手机作为游戏手柄设备。

文章目录 背景界面demo原型图&#xff08;没错&#xff0c;就是它&#xff0c;童年回忆&#xff09; 遇到的问题最终后端demo(甚至比前端逻辑更简单) 背景 突发奇想&#xff0c;想要在前端实现一个fc游戏手柄&#xff0c;然后控制电脑的nes模拟器玩玩魂斗罗。 思路很简单&…

单细胞|MEBOCOST·细胞间代谢通讯

概述 在代谢活跃的细胞中&#xff0c;表达的代谢酶催化代谢反应生成许多代谢物。这些代谢物中的一些可以扩散到细胞外空间并作为信号分子发挥作用。某些细胞外代谢物可以与空间上邻近细胞的感应蛋白结合。我们将分泌代谢物的细胞称为发送细胞&#xff0c;而表达感应蛋白的细胞称…