using SqlSugar; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.Academy.Model { public class Sqlite { private static Sqlite instance; public static Sqlite GetInstance { get; set; } = instance ??= new Sqlite(); private Sqlite() { } public List saveDatas { get; set; } = new List(); static string path { get { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory); return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile\\DB\\data.db"); } } public SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig() { ConnectionString = $"Data Source = {path}", DbType = DbType.Sqlite, IsAutoCloseConnection = true, }); public void Init() { try { if (!File.Exists(path)) { Db.DbMaintenance.CreateDatabase(); Db.CodeFirst.SetStringDefaultLength(100).InitTables(); } else { MessageNotify.GetInstance.ShowRunLog("创建失败"); } } catch (Exception) { throw; } } public bool AddData(SaveData dfb) { try { return Db.Insertable(dfb).ExecuteCommand() > 0; } catch (Exception) { return false; } } public List SelectId(string id) { try { return Db.Queryable().Where(o => o.Id == id).ToList(); } catch (Exception) { return null; } } public List SelectAllName() { return Db.Queryable() .Select(x => x.Name) .Distinct() .ToList(); } } }