ToB企服应用市场:ToB评测及商务社交产业平台

标题: C# WPF 桌面应用程序利用 SQlite 数据库 [打印本页]

作者: 自由的羽毛    时间: 2024-8-5 20:57
标题: 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.        /// <summary>
  2.         ///
  3.         /// </summary>
  4.         /// <param name="file">sqlite databases file </param>
  5.         /// <returns></returns>
  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.               
  19.                 if (res >= 0)
  20.                 {
  21.                     Debug.WriteLine($"{m.name} Delete successed;");
  22.                 }
  23.                 else
  24.                 {
  25.                     Debug.WriteLine($"{m.name} Delete errored;");
  26.                 }
  27.             }
  28.             for (int i = 0; i < 5; i++)
  29.             {
  30.                 Car c = new Car()
  31.                 {
  32.                     id = i + 1,
  33.                     carNumber = "云AAA73" + i,
  34.                     driver = "驾驶员" + i,
  35.                     driverMobile = "1580874631" + i,
  36.                 };
  37.                 int res = -1;
  38.                 if (i % 2 == 0)
  39.                 {
  40.                     string sql = SqlBuilder.GetDeleteSql(c);
  41.                     res = SqliteHelper.Instance.Delete(sql);
  42.                 }
  43.               
  44.                 if (res >= 0)
  45.                 {
  46.                     Debug.WriteLine($"{c.carNumber} Delete successed;");
  47.                 }
  48.                 else
  49.                 {
  50.                     Debug.WriteLine($"{c.carNumber} Delete errored;");
  51.                 }
  52.             }
  53.         }
复制代码
查询并在日志在打印内容


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

代码仓库:sqlite_demo: C# WPF 桌面应用程序,数据存储利用Sqlite ,这是一个数据基本操作的基本Demo
感谢各位朋友的阅读,有不足之处,望指正。

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




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4