终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

85 lines
2.2 KiB

  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BPASmartClient.Academy.Model
  9. {
  10. public class Sqlite
  11. {
  12. private static Sqlite instance;
  13. public static Sqlite GetInstance { get; set; } = instance ??= new Sqlite();
  14. private Sqlite() { }
  15. public List<SaveData> saveDatas { get; set; } = new List<SaveData>();
  16. static string path
  17. {
  18. get
  19. {
  20. Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory);
  21. return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile\\DB\\data.db");
  22. }
  23. }
  24. public SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
  25. {
  26. ConnectionString = $"Data Source = {path}",
  27. DbType = DbType.Sqlite,
  28. IsAutoCloseConnection = true,
  29. });
  30. public void Init()
  31. {
  32. try
  33. {
  34. if (!File.Exists(path))
  35. {
  36. Db.DbMaintenance.CreateDatabase();
  37. Db.CodeFirst.SetStringDefaultLength(100).InitTables<SaveData>();
  38. }
  39. else
  40. {
  41. MessageNotify.GetInstance.ShowRunLog("创建失败");
  42. }
  43. }
  44. catch (Exception)
  45. {
  46. throw;
  47. }
  48. }
  49. public bool AddData(SaveData dfb)
  50. {
  51. try
  52. {
  53. return Db.Insertable(dfb).ExecuteCommand() > 0;
  54. }
  55. catch (Exception)
  56. {
  57. return false;
  58. }
  59. }
  60. public List<SaveData> SelectId(string id)
  61. {
  62. try
  63. {
  64. return Db.Queryable<SaveData>().Where(o => o.Id == id).ToList();
  65. }
  66. catch (Exception)
  67. {
  68. return null;
  69. }
  70. }
  71. public List<String> SelectAllName()
  72. {
  73. return Db.Queryable<SaveData>()
  74. .Select(x => x.Name)
  75. .Distinct()
  76. .ToList();
  77. }
  78. }
  79. }