private void CreateOrConnect(ConnectMode mode) { bool result = true; try { switch (mode) { case ConnectMode.Create: if (!CheckPathExists()) { return; } if (File.Exists(this.Database)) { if (MessageBoxEx.Show("The file already exists. Are you sure you want to overwrite it?", "Create Error", MessageBoxButton.YesNo, MessageBoxImage.Error) == MessageBoxResult.No) { return; } File.Delete(this.Database); } var sql = new SqliteDatabase() { DatabasePath = this.Database, Password = this.Password }; sql.Create(); result = true; break; case ConnectMode.Connect: if (File.Exists(this.Database) == false) { MessageBoxEx.Show("The file doesn't exist. In order to open a database you must specify a SQL Lite database file that exists", "Restore Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } break; case ConnectMode.Restore: if (File.Exists(this.Database) == false) { MessageBoxEx.Show("The file doesn't exist. In order to restore the database you must specify a SQL Lite backup file that exists", "Restore Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } break; } } catch (OperationCanceledException) { result = false; } catch (Exception ex) { MessageBoxEx.Show(ex.Message, "Error Creating Database", MessageBoxButton.OK, MessageBoxImage.Error); result = false; } this.Cursor = Cursors.Arrow; if (result) { this.DialogResult = result; } }
public void TestSqlMapping() { MyMoney m = new MyMoney(); Account a = new Account(); a.Name = "Bank of America"; m.Accounts.AddAccount(a); Transaction t = new Transaction(); t.Account = a; t.Date = DateTime.Now; t.Amount = -65.00M; t.Payee = m.Payees.FindPayee("Costco", true); t.Category = m.Categories.FindCategory("Food"); t.Memo = "something"; m.Transactions.AddTransaction(t); string dbPath = System.IO.Path.GetTempPath(); string dbName = System.IO.Path.Combine(dbPath, "Test.MyMoney.db"); if (System.IO.File.Exists(dbName)) { System.IO.File.Delete(dbName); } SqliteDatabase db = new SqliteDatabase(); db.DatabasePath = dbName; db.Create(); db.Save(m); // test we can add a column to the Transactions table. TableMapping mapping = new TableMapping() { TableName = "Transactions" }; mapping.ObjectType = typeof(Transaction); var c = new ColumnMapping() { ColumnName = "Foo", SqlType = typeof(SqlChars), AllowNulls = true, MaxLength = 20 }; mapping.Columns.Add(c); db.CreateOrUpdateTable(mapping); // make sure the new column exists var metadata = db.LoadTableMetadata(mapping.TableName); var d = (from i in metadata.Columns where i.ColumnName == "Foo" select i).FirstOrDefault(); Assert.IsNotNull(d); Assert.AreEqual(d.MaxLength, 20); // test we can change the max length c.MaxLength = 50; db.CreateOrUpdateTable(mapping); // make sure the new column has new length metadata = db.LoadTableMetadata(mapping.TableName); d = (from i in metadata.Columns where i.ColumnName == "Foo" select i).FirstOrDefault(); Assert.IsNotNull(d); Assert.AreEqual(d.MaxLength, 50); // test we can drop the column mapping.Columns.Remove(c); db.CreateOrUpdateTable(mapping); // verify it's gone! metadata = db.LoadTableMetadata(mapping.TableName); d = (from i in metadata.Columns where i.ColumnName == "Foo" select i).FirstOrDefault(); Assert.IsNull(d); // test we can still load the modified database! MyMoney test = db.Load(null); Account b = test.Accounts.FindAccount(a.Name); Assert.IsNotNull(b); Transaction s = test.Transactions.GetTransactionsFrom(b).FirstOrDefault(); Assert.IsNotNull(s); Assert.AreEqual(t.Date, s.Date); Assert.AreEqual(t.Amount, s.Amount); Assert.AreEqual(t.CategoryFullName, s.CategoryFullName); Assert.AreEqual(t.PayeeName, s.PayeeName); Assert.AreEqual(t.Memo, s.Memo); }
private bool ProcessFile(string file) { ShowStatus(string.Format(loadingStatusPrompt, file)); try { if (file.EndsWith(".db", StringComparison.OrdinalIgnoreCase)) { string userName = null; string password = null; string path = Path.GetFullPath(file); bool cancelled = false; dispatcher.Invoke(new Action(() => { PasswordWindow w = new PasswordWindow(); Paragraph p = (Paragraph)w.IntroMessagePrompt.Document.Blocks.FirstBlock; Run run = (Run)p.Inlines.FirstInline; run.Text = "Please enter password for the imported database"; if (w.ShowDialog() == true) { userName = w.UserName; password = w.PasswordConfirmation; path = Path.GetFullPath(file); } else { ShowStatus("Import cancelled."); cancelled = true; } })); if (!cancelled) { SqliteDatabase database = new SqliteDatabase() { DatabasePath = path, UserId = userName, Password = password }; database.Create(); string attachmentsDir = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + ".Attachments"); if (Directory.Exists(attachmentsDir)) { MyMoney newMoney = database.Load(this); AttachmentManager importAttachments = new AttachmentManager(newMoney); importAttachments.AttachmentDirectory = attachmentsDir; ImportMoneyFile(newMoney, importAttachments); } else { // todo: prompt user for attachments? } } else { return(false); } } else { ShowStatus("Import only supports sqllite money files"); return(false); } } catch (Exception ex) { ShowStatus("Error: " + ex.Message); return(false); } return(true); }