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.在程序中连接数据库,并做增删改查操作

cs 复制代码
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;
            }
        }

    }
相关推荐
John_Hui3 小时前
Windows 10 安装版本:mysql-8.1.0-winx64.zip
数据库·windows·mysql
莫晓燚3 小时前
双机部署若依 (RuoYi‑Vue) 传统环境部署---实战
linux·运维·数据库·redis·mysql·nginx·ruoyi
暴走约伯3 小时前
【VFX工具链源码拆解 02】告别机械劳动(上):一键统改百个子粒子颜色与批量重命名
3d·unity·c#·材质
灵晔君5 小时前
【MySQL】数据库基础
数据库·mysql·adb
信息安全失业大专人员5 小时前
每日了解一个智能体-WorkBuddy
开发语言·ai·c#·智能体
香蕉大香蕉(06敬畏市场)5 小时前
MySQL 单点详解(03)
java·数据库·mysql·面试·八股·大厂实习
呆呆敲代码的小Y5 小时前
【实战精选面试题】C# 面试题详细解答,按难易程度排序
android·java·游戏·c#·游戏开发·unity面试题·c#面试题
淡海水6 小时前
09-01-内存-Unity资源生命周期全景
unity·c#·游戏引擎·.netcore·gc
杨云龙UP7 小时前
MySQL 主从复制管理:暂停、恢复、清理主从关系及命令执行位置
运维·数据库·mysql·主从配置·主从命令·reset replica·清除复制
霂鳯7 小时前
新手勇闯网络安全 | MySQL复习
mysql·安全·web安全