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

打印 上一主题 下一主题

主题 1005|帖子 1005|积分 3015

文章具体介绍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.在程序中连接数据库,并做增编削查操作
  1. public class users//此处的类名users需要和数据库中的数据表的表名相同
  2.     {
  3.         public int user_id { get; set; }
  4.         public string user_name { get; set; }
  5.         public int user_age { get; set; }
  6.         public string user_email { get; set; }
  7.     }
  8. public class SqlSugarHelper
  9.     {
  10.         private static readonly string connectionString = "Server=10.11.10.105;Database=Info_Manage;User ID=tom;Password=123456;";
  11.         
  12.         private static SqlSugarClient GetDbClient()
  13.         {
  14.             return new SqlSugarClient(new ConnectionConfig()
  15.             {
  16.                 ConnectionString = connectionString,
  17.                 DbType = DbType.MySql, // 设置数据库类型
  18.                 IsAutoCloseConnection = true, // 自动关闭连接
  19.                 InitKeyType = InitKeyType.Attribute // 初始化主键和自增列信息的方式
  20.             });
  21.         }
  22.         public static List<users> GetAllUsers()//此行代码中的List<users>中的users为数据库中用户表的表名,下面方法中也一样
  23.         {
  24.             try
  25.             {
  26.                 using (var db = GetDbClient())
  27.                 {
  28.                     return db.Queryable<users>().ToList();
  29.                 }
  30.             }
  31.             catch (Exception ex)
  32.             {
  33.                 // 记录日志或处理异常
  34.                 Console.WriteLine($"获取所有用户时发生异常: {ex.Message}");
  35.                 throw;
  36.             }
  37.         }
  38.         public static users GetUserById(int id)
  39.         {
  40.             try
  41.             {
  42.                 using (var db = GetDbClient())
  43.                 {
  44.                     return db.Queryable<users>().Where(u => u.user_id == id).First();
  45.                 }
  46.             }
  47.             catch (Exception ex)
  48.             {
  49.                 // 记录日志或处理异常
  50.                 Console.WriteLine($"获取用户时发生异常: {ex.Message}");
  51.                 throw;
  52.             }
  53.         }
  54.         public static bool InsertUser(users user)
  55.         {
  56.             try
  57.             {
  58.                 using (var db = GetDbClient())
  59.                 {
  60.                     return db.Insertable(user).ExecuteCommand() > 0;
  61.                 }
  62.             }
  63.             catch (Exception ex)
  64.             {
  65.                 // 记录日志或处理异常
  66.                 Console.WriteLine($"插入用户时发生异常: {ex.Message}");
  67.                 throw;
  68.             }
  69.         }
  70.         public static bool UpdateUser(users user)
  71.         {
  72.             try
  73.             {
  74.                 using (var db = GetDbClient())
  75.                 {
  76.                     return db.Updateable(user).ExecuteCommand() > 0;
  77.                 }
  78.             }
  79.             catch (Exception ex)
  80.             {
  81.                 // 记录日志或处理异常
  82.                 Console.WriteLine($"更新用户时发生异常: {ex.Message}");
  83.                 throw;
  84.             }
  85.         }
  86.         public static bool DeleteUser(int id)
  87.         {
  88.             try
  89.             {
  90.                 using (var db = GetDbClient())
  91.                 {
  92.                     return db.Deleteable<users>().Where(u => u.user_id == id).ExecuteCommand() > 0;
  93.                 }
  94.             }
  95.             catch (Exception ex)
  96.             {
  97.                 // 记录日志或处理异常
  98.                 Console.WriteLine($"删除用户时发生异常: {ex.Message}");
  99.                 throw;
  100.             }
  101.         }
  102.     }
复制代码






免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表