• C# WPF 桌面应用程序使用 SQlite 数据库


            我们在开发 WPF 桌面应用程序时,数据库存的使用是必不可少的,除非你的应用没有数据存储的需求,有了数据存储需求,我们就会面临使用什么样的数据库的选择问题,我的选择方案是,单机版的应用我优先选择 Sqlite,如果钓多台电脑需要数据共享我优先MySql 8.0+,Sqlite 和MySql 都支持标准的SQL 结构查询语句,数据库的切换也不需要额外大量的开发工作。单机版使用Sqlite ,免去 MySql 安装过程,减少用户的操作,降低使用门槛。

            之前的很多应用都是使用 MySql 。现在记录下对 Sqlite 数据的基本使用。

    准备两张表和C# 实体类,代码如下
    1. public class BaseEntity
    2. {
    3. public Int64 id;
    4. public Int64 Id
    5. {
    6. get
    7. {
    8. return id;
    9. }
    10. set
    11. {
    12. id = value;
    13. }
    14. }
    15. }
    1. //车辆作息
    2. public class Car : BaseEntity
    3. {
    4. public string carNumber;
    5. public decimal traeWeight;
    6. public string driver;
    7. public string driverMobile;
    8. }
    1. //货物信息 Material
    2. public class Marteral :BaseEntity
    3. {
    4. public string name;
    5. public string firstCase;
    6. }

     

    对应的表结构
    1. CREATE TABLE "main"."marteral" (
    2. "id" INTEGER NOT NULL,
    3. "name" TEXT(255) DEFAULT NULL,
    4. "first_case" TEXT(255) DEFAULT NULL,
    5. PRIMARY KEY ("id" ASC)
    6. );
    7. CREATE TABLE "main"."car" (
    8. "id" bigint NOT NULL,
    9. "car_number" TEXT(255) DEFAULT NULL,
    10. "trae_weight" real(10,3) DEFAULT '0.000',
    11. "driver" TEXT(255) DEFAULT NULL,
    12. "driver_mobile" TEXT(255) DEFAULT NULL,
    13. "driver_idnumber" TEXT(255) DEFAULT NULL,
    14. PRIMARY KEY ("id" ASC)
    15. )
    16. ;
    第一步 在Nuget中 引入 Sqlite的库。

    的代码中引入 命名空间

    1. using System.Data;
    2. using Microsoft.Data.Sqlite;
    第二步 连接Sqlite。
    构建连接字符串
    1. ///
    2. ///
    3. ///
    4. /// sqlite databases file
    5. ///
    6. private static string GetConnString(string file)
    7. {
    8. var connStr = new SqliteConnectionStringBuilder()
    9. {
    10. DataSource = file,
    11. Pooling = true,
    12. // 注意 Mode的值 , SqliteOpenMode.ReadWriteCreate表示不存在文件时
    13. //会自动创建
    14. Mode = SqliteOpenMode.ReadWriteCreate,
    15. }.ConnectionString;
    16. return connStr;
    17. }
    连接
    1. public bool Connection()
    2. {
    3. bool res = false;
    4. //db file is not exist,
    5. using (SqliteConnection connection = new SqliteConnection(GetConnString(dbfile)))
    6. {
    7. if (connection.State != ConnectionState.Open)
    8. {
    9. connection.Open();
    10. res = connection.State == ConnectionState.Open;
    11. }
    12. }
    13. return res;
    14. }
    第三步  使用。

            添加数据
    1. //各添加10万条数
    2. private void InsertBtn_Click(object sender, RoutedEventArgs e)
    3. {
    4. int total = 100000;
    5. for (int i = 0; i < total; i++)
    6. {
    7. Marteral m = new Marteral()
    8. {
    9. id = i+1,
    10. name = "原煤"+i,
    11. firstCase = "YM"+i,
    12. };
    13. int res = -1;
    14. if(i% 2 == 0)
    15. {
    16. string sql = SqlBuilder.GetInsertSql(m);
    17. res = SqliteHelper.Instance.Insert(sql);
    18. }
    19. else
    20. {
    21. res = SqliteHelper.Instance.Insert(m);
    22. }
    23. if(res >= 0)
    24. {
    25. Debug.WriteLine($"{m.name} inseert successed;");
    26. }
    27. else
    28. {
    29. Debug.WriteLine($"{m.name} inseert errored;");
    30. }
    31. }
    32. for (int i = 0; i < total; i++)
    33. {
    34. Car c= new Car()
    35. {
    36. id = i+1,
    37. carNumber = "云DDD73" + i,
    38. driver = "驾驶员" + i,
    39. driverMobile = "1580874631" +i,
    40. };
    41. int res = -1;
    42. if (i % 2 == 0)
    43. {
    44. string sql = SqlBuilder.GetInsertSql(c);
    45. res = SqliteHelper.Instance.Insert(sql);
    46. }
    47. else
    48. {
    49. res = SqliteHelper.Instance.Insert(c);
    50. }
    51. if (res >= 0)
    52. {
    53. Debug.WriteLine($"{c.carNumber} inseert successed;");
    54. }
    55. else
    56. {
    57. Debug.WriteLine($"{c.carNumber} inseert errored;");
    58. }
    59. }
    60. }
            修改数据
    1. private void UpdateBtn_Click(object sender, RoutedEventArgs e)
    2. {
    3. for (int i = 0; i < 5; i++)
    4. {
    5. Marteral m = new Marteral()
    6. {
    7. id = i + 1,
    8. name = "精煤煤" + i,
    9. firstCase = "JM" + i,
    10. };
    11. int res = -1;
    12. if (i % 2 == 0)
    13. {
    14. string sql = SqlBuilder.GetUpdateSql(m);
    15. res = SqliteHelper.Instance.Update(sql);
    16. }
    17. else
    18. {
    19. res = SqliteHelper.Instance.Update(m);
    20. }
    21. if (res >= 0)
    22. {
    23. Debug.WriteLine($"{m.name} Update successed;");
    24. }
    25. else
    26. {
    27. Debug.WriteLine($"{m.name} Update errored;");
    28. }
    29. }
    30. for (int i = 0; i < 5; i++)
    31. {
    32. Car c = new Car()
    33. {
    34. id = i + 1,
    35. carNumber = "云AAA73" + i,
    36. driver = "驾驶员" + i,
    37. driverMobile = "1580874631" + i,
    38. };
    39. int res = -1;
    40. if (i % 2 == 0)
    41. {
    42. string sql = SqlBuilder.GetUpdateSql(c);
    43. res = SqliteHelper.Instance.Update(sql);
    44. }
    45. else
    46. {
    47. res = SqliteHelper.Instance.Update(c);
    48. }
    49. if (res >= 0)
    50. {
    51. Debug.WriteLine($"{c.carNumber} Update successed;");
    52. }
    53. else
    54. {
    55. Debug.WriteLine($"{c.carNumber} Update errored;");
    56. }
    57. }
    58. }

            删除数据

            

    1. // id % 2 == 0 的数据删除
    2. private void DeleteBtn_Click(object sender, RoutedEventArgs e)
    3. {
    4. for (int i = 0; i < 5; i++)
    5. {
    6. Marteral m = new Marteral()
    7. {
    8. id = i + 1,
    9. name = "精煤煤" + i,
    10. firstCase = "JM" + i,
    11. };
    12. int res = -1;
    13. if (i % 2 == 0)
    14. {
    15. string sql = SqlBuilder.GetDeleteSql(m);
    16. res = SqliteHelper.Instance.Delete(sql);
    17. }
    18. if (res >= 0)
    19. {
    20. Debug.WriteLine($"{m.name} Delete successed;");
    21. }
    22. else
    23. {
    24. Debug.WriteLine($"{m.name} Delete errored;");
    25. }
    26. }
    27. for (int i = 0; i < 5; i++)
    28. {
    29. Car c = new Car()
    30. {
    31. id = i + 1,
    32. carNumber = "云AAA73" + i,
    33. driver = "驾驶员" + i,
    34. driverMobile = "1580874631" + i,
    35. };
    36. int res = -1;
    37. if (i % 2 == 0)
    38. {
    39. string sql = SqlBuilder.GetDeleteSql(c);
    40. res = SqliteHelper.Instance.Delete(sql);
    41. }
    42. if (res >= 0)
    43. {
    44. Debug.WriteLine($"{c.carNumber} Delete successed;");
    45. }
    46. else
    47. {
    48. Debug.WriteLine($"{c.carNumber} Delete errored;");
    49. }
    50. }
    51. }
    查询并在日志在打印内容

    1. private void SelectBtn_Click(object sender, RoutedEventArgs e)
    2. {
    3. string sql = SqlBuilder.GetSelectSql("car", "", "");
    4. List cars = SqliteHelper.Instance.Select(sql);
    5. cars.ForEach((c) => { Debug.WriteLine(c.carNumber+" trae:"+c.traeWeight); });
    6. string sql2 = SqlBuilder.GetSelectSql("marteral", "", "");
    7. List ms = SqliteHelper.Instance.Select(sql2);
    8. ms.ForEach((m) => { Debug.WriteLine(m.name); });
    9. }
    最后

    代码仓库:sqlite_demo: C# WPF 桌面应用程序,数据存储使用Sqlite ,这是一个数据基本操作的基本Demo

    感谢各位朋友的阅读,有不足之处,望指正。

  • 相关阅读:
    学术汇报(academic presentation)/PPT应该怎么做?
    sql10(Leetcode1661每台机器的进程平均运行时间)
    (二十七)大数据实战——hbase高可用集群安装与部署
    解决github打开慢的问题
    基于Docker搭建ELK(Elasticsearch、Logstash、Kibana)日志框架
    含文档+PPT+源码等]精品基于SSM的公交线路查询系统[包运行成功]程序设计源码计算机毕设
    推荐10个地推拉新app推广接单平台,都是一手单 官签渠道
    iOS APP 转让避坑指南
    stm32工程中的DebugConfig、Listings和Objects 3个文件夹
    双线性池化(Bilinear Pooling)
  • 原文地址:https://blog.csdn.net/badxnui/article/details/136216049