public List<InfoItem> GetAllInfo() { var constants = new Dictionary<short, string>(); using (var client = new SqlDbCeClient(this.CurrentUser.ConnectionString)) { client.CommandText = "SELECT * FROM [info] ORDER BY [id_info]"; var result = client.GetEntities<InfoItem>(); // добвавляем к результату имена констант this.GetAllConstants(null, typeof(InfoId), constants); foreach (var item in result) { if (constants.ContainsKey(item.Id)) { item.Name = constants[item.Id]; } else { if (item.Id <= 99) { item.Name = String.Format("Initial.{0}", item.Id); } else if (item.Id > 99 && item.Id <= 199) { item.Name = String.Format("Stat.{0}", item.Id); } else if (item.Id > 199 && item.Id <= 499) { item.Name = String.Format("Reserved.{0}", item.Id); } else if (item.Id > 499 && item.Id <= 519) { item.Name = String.Format("Settings.Desktop.{0}", item.Id); } else if (item.Id > 519 && item.Id <= 559) { item.Name = String.Format("Settings.Desktop.Incomes.{0}", item.Id); } else if (item.Id > 559 && item.Id <= 599) { item.Name = String.Format("Settings.Desktop.Expenses.{0}", item.Id); } else if (item.Id > 599 && item.Id <= 619) { item.Name = String.Format("Settings.Mobile.{0}", item.Id); } else if (item.Id > 619 && item.Id <= 659) { item.Name = String.Format("Settings.Mobile.Incomes.{0}", item.Id); } else if (item.Id > 659 && item.Id <= 699) { item.Name = String.Format("Settings.Mobile.Expenses.{0}", item.Id); } else if (item.Id > 699 && item.Id <= 719) { item.Name = String.Format("Settings.Web.{0}", item.Id); } else if (item.Id > 719 && item.Id <= 759) { item.Name = String.Format("Settings.Web.Incomes.{0}", item.Id); } else if (item.Id > 759 && item.Id <= 799) { item.Name = String.Format("Settings.Web.Expenses.{0}", item.Id); } else if (item.Id > 799 && item.Id <= 999) { item.Name = String.Format("Settings.Reserved.{0}", item.Id); } else if (item.Id > 999 && item.Id <= 9999) { item.Name = String.Format("Custom.{0}", item.Id); } else { item.Name = String.Format("Unknown.{0}", item.Id); } } } // -- return result; } }
public void EntitiesTest() { string password = ""; string filePath = Path.Combine(App.CurrentPath, "test.sdf"); if (File.Exists(filePath)) { File.Delete(filePath); } string connectionString = String.Format("Data Source={0}; password={1}", filePath, password); SqlDbCeClient.CreateDatabase(connectionString); using (var client = new SqlDbCeClient(connectionString)) { client.CommandText = @"CREATE TABLE [accounts] ( [id_accounts] int IDENTITY (1,1) NOT NULL, [id_account_types] int NOT NULL, [id_currencies] nvarchar(3) NOT NULL, [id_icons] int DEFAULT(0) NOT NULL, [account_name] nvarchar(100) NOT NULL, [account_details] nvarchar(4000) NOT NULL, [total_income_entries] int DEFAULT (0) NOT NULL, [total_expense_entries] int DEFAULT (0) NOT NULL, [last_operation] datetime NULL, [date_created] datetime DEFAULT (GETDATE()) NOT NULL );"; client.ExecuteNonQuery(); client.CommandText = "ALTER TABLE [accounts] ADD CONSTRAINT [PK_accounts] PRIMARY KEY ([id_accounts]);"; client.ExecuteNonQuery(); } using (var client = new SqlDbCeClient(connectionString)) { client.QueryProcessing += (sender, e) => { var args = (QueryProcessingEventArgs)e; Console.WriteLine(args.Status); }; var a = new Account(); a.CurrencyCode = "RUB"; a.AccountTypeId = 1; a.Details = "test"; a.IconId = 123; a.Id = 0; a.Name = "проверка"; for (int i = 1; i <= 10; i++) { client.SaveEntities<Account>(a); Console.WriteLine("Id: {0} => {1}", a.Id, a.Status); Assert.AreEqual(a.Id, i); Assert.IsTrue(a.Status == EntityStatus.Created); a.Id = 0; } var aaa = client.GetEntities<Account>("SELECT * FROM accounts"); Console.WriteLine("Count: {0}", aaa.Count); Assert.IsTrue(aaa.Count == 10); var a2 = client.GetEntity<Account>("SELECT * FROM accounts WHERE id_accounts = 3"); Assert.IsTrue(a2.Status == EntityStatus.Loaded); a2.Name = "Тест123"; a2.LastOperation = DateTime.Now; client.SaveEntities<Account>(a2); Assert.AreEqual(a2.Name, "Тест123"); Assert.AreEqual(a2.Id, 3); Assert.IsTrue(a2.Status == EntityStatus.Updated); a2 = client.GetEntity<Account>("SELECT * FROM accounts WHERE id_accounts = 3"); Assert.AreEqual(a2.Name, "Тест123"); Assert.AreEqual(a2.Id, 3); a2 = client.GetEntity<Account>("SELECT * FROM accounts WHERE id_accounts = 4"); Assert.AreNotEqual(a2.Name, "Тест123"); Assert.AreNotEqual(a2.Id, 3); a2 = client.GetEntity<Account>("SELECT * FROM accounts WHERE id_accounts = 123"); Assert.IsTrue(a2 == null); } }