当前位置: 首页 > article >正文

c# wpf 开发中安装使用SqlSugar操作MySql数据库具体操作步骤保姆级教程

文章详细介绍wpf 开发中安装使用SqlSugar操作MySql数据库具体操作步骤,以及需要同时安装的其他工具包

首先声明具体开发环境:.NetFramework 4.7.2

安装的工具包版本为:SqlSugar 5.1.4.180,

同时还需要安装MySql.Data 和Newtonsoft.Json,我安装的是MySql.Data 8.0.33和 Newtonsoft.Json 13.0.3

1.创建一个wpf  .NetFramework项目,选择.NetFramework 4.7.2

2.右键管理方案名称,点击管理NuGet程序包

3.搜索并安装以下三个工具包,注意看右边的详细信息判断安装的具体版本是否支持自己的.NetFramework版本,有的写的不清楚,可以去网上搜一下,问问deepseek也行

4.在程序中连接数据库,并做增删改查操作

public class users//此处的类名users需要和数据库中的数据表的表名相同
    {
        public int user_id { get; set; }
        public string user_name { get; set; }
        public int user_age { get; set; }
        public string user_email { get; set; }
    }

public class SqlSugarHelper
    {
        private static readonly string connectionString = "Server=10.11.10.105;Database=Info_Manage;User ID=tom;Password=123456;";
        
        private static SqlSugarClient GetDbClient()
        {
            return new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString,
                DbType = DbType.MySql, // 设置数据库类型
                IsAutoCloseConnection = true, // 自动关闭连接
                InitKeyType = InitKeyType.Attribute // 初始化主键和自增列信息的方式
            });
        }

        public static List<users> GetAllUsers()//此行代码中的List<users>中的users为数据库中用户表的表名,下面方法中也一样
        {
            try
            {
                using (var db = GetDbClient())
                {
                    return db.Queryable<users>().ToList();
                }
            }
            catch (Exception ex)
            {
                // 记录日志或处理异常
                Console.WriteLine($"获取所有用户时发生异常: {ex.Message}");
                throw;
            }
        }
        public static users GetUserById(int id)
        {
            try
            {
                using (var db = GetDbClient())
                {
                    return db.Queryable<users>().Where(u => u.user_id == id).First();
                }
            }
            catch (Exception ex)
            {
                // 记录日志或处理异常
                Console.WriteLine($"获取用户时发生异常: {ex.Message}");
                throw;
            }
        }

        public static bool InsertUser(users user)
        {
            try
            {
                using (var db = GetDbClient())
                {
                    return db.Insertable(user).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                // 记录日志或处理异常
                Console.WriteLine($"插入用户时发生异常: {ex.Message}");
                throw;
            }
        }

        public static bool UpdateUser(users user)
        {
            try
            {
                using (var db = GetDbClient())
                {
                    return db.Updateable(user).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                // 记录日志或处理异常
                Console.WriteLine($"更新用户时发生异常: {ex.Message}");
                throw;
            }
        }

        public static bool DeleteUser(int id)
        {
            try
            {
                using (var db = GetDbClient())
                {
                    return db.Deleteable<users>().Where(u => u.user_id == id).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                // 记录日志或处理异常
                Console.WriteLine($"删除用户时发生异常: {ex.Message}");
                throw;
            }
        }

    }


http://www.kler.cn/a/574181.html

相关文章:

  • 智慧校园可视化:开启校园管理的数字化新未来
  • 2005-2019年各省城镇人口数据
  • 【hello git】git 扫盲(add、commit、push、reset、status、log、checkout)
  • 【论文分享】推理大模型Post-Training技术的全面综述
  • Java数组详解/从JVM理解数组/数组反转/随机排名/数组在计算机如何存储
  • Unity Shader 学习15:可交互式雪地流程
  • Codepen和tailwindcss 进行UI布局展示
  • VBA第十八期 如何获得WPS中已经安装字体的列表
  • 在vue2项目中el-table表格的表头和内容错位问题
  • 责任链模式:让请求在链条中流动
  • 基于GeoTools的GIS专题图自适应边界及高宽等比例生成实践
  • pytorch3d学习(二)——安装与纹理显示demo测试
  • 2025年中职大数据应用与服务竞赛培训方案分享
  • 介绍一个能支持高带宽的EDID编辑软件
  • 23种设计模式之《状态模式(State)》在c#中的应用及理解
  • Spring Boot整合Resilience4j教程
  • 大模型基础-深度解析-什么是语言模型及大模型相关历史回顾
  • Unity帧同步与状态同步混合架构开发指南
  • python之爬虫入门实例
  • 永恒之塔鼠标卡顿移动鼠标卡屏的问题