/// <summary> /// Saves the application configuration to a database. /// </summary> /// <param name="Model">The data to save to the database.</param> /// <param name="path">The path to the database (Default = "Share.DB").</param> public static void SaveToDB(SharesDataModel model, string path = DEFAULTPATH) { // create a connection to the database using (SQLiteConnection con = new SQLiteConnection(path)) { // create the tabels con.CreateTable <Share>(); con.CreateTable <Order>(); con.CreateTable <ShareValue>(); // remove all entries for the tables con.DeleteAll <Share>(); con.DeleteAll <Order>(); con.DeleteAll <ShareValue>(); if (model != null) { foreach (var s in model.Shares) { // populate the share table with the values of the datamodel con.Insert(new Share() { ISIN = s.ISIN, ShareName = s.ShareName, WebSite = s.WebSite, WKN = s.WKN, }, typeof(Share)); } foreach (var o in model.Orders) { // populate the order table with the values of the datamodel con.Insert(new Order() { ISIN = o.ISIN, Amount = o.Amount, Date = o.Date, OrderExpenses = o.OrderExpenses, OrderType = o.OrderType, SharePrice = o.SharePrice, }, typeof(Order)); } foreach (var v in model.ShareValues) { // populate the share table with the values of the datamodel con.Insert(new ShareValue() { Date = v.Date, ISIN = v.ISIN, Price = v.Price, }, typeof(ShareValue)); } } } }
/// <summary> /// Reads the application configuration from the database. /// </summary> /// <param name="path">The path to the database (Default = "Share.DB").</param> /// <returns>The data to read from the database.</returns> public static SharesDataModel ReadFromDB(string path = DEFAULTPATH) { // create a new datamodel SharesDataModel model = new SharesDataModel(); // connect to the database using (SQLiteConnection con = new SQLiteConnection(path)) { // get the tables of the database con.CreateTable <Share>(); con.CreateTable <Order>(); con.CreateTable <ShareValue>(); // populate the model with the table contents model.Shares = con.Table <Share>().ToList(); model.Orders = con.Table <Order>().ToList(); model.ShareValues = con.Table <ShareValue>().ToList(); } return(model); }