private void save ButtonClickobject sender Routed Event Args
Поведение кнопки «сохранить» private void save. Button_Click(object sender, Routed. Event. Args e) { save. Text("notes. txt", jot. Text. Box. Text); } § § 6 Windows Phone
Метод save. Text private void save. Text(string filename, string text) { using (Isolated. Storage. File isf = Isolated. Storage. File. Get. User. Store. For. Application()) { using (Isolated. Storage. File. Stream raw. Stream = isf. Create. File(filename)) { Stream. Writer writer = new Stream. Writer(raw. Stream); writer. Write(text); writer. Close(); } } } § 7 Windows Phone
Класс Isolated. Storage. File private void save. Text(string filename, string text) { using (Isolated. Storage. File isf = Isolated. Storage. File. Get. User. Store. For. Application()) { using (Isolated. Storage. File. Stream raw. Stream = isf. Create. File(filename)) { Stream. Writer writer = new Stream. Writer(raw. Stream); writer. Write(text); writer. Close(); } } } § 8 Windows Phone
Создание файла private void save. Text(string filename, string text) { using (Isolated. Storage. File isf = Isolated. Storage. File. Get. User. Store. For. Application()) { using (Isolated. Storage. File. Stream raw. Stream = isf. Create. File(filename)) { Stream. Writer writer = new Stream. Writer(raw. Stream); writer. Write(text); writer. Close(); } } } § 9 Windows Phone
Запись данных в файл private void save. Text(string filename, string text) { using (Isolated. Storage. File isf = Isolated. Storage. File. Get. User. Store. For. Application()) { using (Isolated. Storage. File. Stream raw. Stream = isf. Create. File(filename)) { Stream. Writer writer = new Stream. Writer(raw. Stream); writer. Write(text); writer. Close(); } } } § 10 Windows Phone
Поведение кнопки «загрузить» private void load. Button_Click(object sender, Routed. Event. Args e) { string text; if ( load. Text("notes. txt", out text ) ) { notes. Text. Box. Text = text; } else { notes. Text. Box. Text = "Напишите здесь что-нибудь. . "; } } § § 11 Windows Phone
Загрузка данных из хранилища try { using (Isolated. Storage. File. Stream raw. Stream = isf. Open. File(filename, System. IO. File. Mode. Open)) { Stream. Reader reader = new Stream. Reader(raw. Stream); result = reader. Read. To. End(); reader. Close(); } } catch { return false; } § 12 Windows Phone
Сохранение настроек private void save. Text(string filename, string text) { Isolated. Storage. Settings isolated. Store = Isolated. Storage. Settings. Application. Settings; isolated. Store. Remove(filename); isolated. Store. Add(filename, text); isolated. Store. Save(); } § § 16 Windows Phone
Загрузка данных из хранилища private bool load. Text(string filename, out string result) { Isolated. Storage. Settings isolated. Store = Isolated. Storage. Settings. Application. Settings; result = ""; try { result = (string)isolated. Store[filename]; } catch { return false; } return true; } 19 Windows Phone
Управление загрузкой result = ""; try { result = (string) isolated. Store[filename]; } catch { return false; } § § 20 Windows Phone
Таблица Customers Customer. ID Name Address Bank. Details 123456 Rob 18 Pussycat Mews Nut East Bank 654322 Jim 10 Motor Drive Big Fall Bank 111111 Ethel 4 Funny Address Strange bank § § § 25 Windows Phone
Таблицы Products и Orders Product. ID 1001 1002 1003 Order. ID 1 2 3 Product. Name Windows Phone 7 Cheese grater Boat hook Customer. ID Product. ID 123456 1001 111111 1002 654322 1003 Supplier Microsoft Cheese Industries John’s Dockyard Quantity 1 5 4 Order. Date 21/10/2010 10/10/2010 01/09/2010 Price 200 2 20 Status Shipped On order § § 26 Windows Phone
Базы данных и классы public class Customer { public int ID { get; set; } public string Name { get; set; } public string Address { get; set; } public string Bank. Details { get; set; } } § § 28 Windows Phone
Технология LINQ § § § 29 Windows Phone
Пространства имён LINQ using using System. Linq; System. Data. Linq. Mapping; System. Component. Model; System. Collections. Object. Model; § § 30 Windows Phone
Создание таблицы Customers § § § 31 Windows Phone
Атрибут Table [Table] public class Customer : INotify. Property. Changed, INotify. Property. Changing { // здесь описывается структура класса } § § 32 Windows Phone
События изменения данных [Table] public class Customer : INotify. Property. Changed, INotify. Property. Changing { // здесь описывается структура класса } § § 33 Windows Phone
Секции get и set в свойствах private string name. Value; public string Name { get { return name. Value; } set { // код секции set } } § 34 Windows Phone
Код секции set { if (Property. Changing != null) { Property. Changing(this, new Property. Changing. Event. Args("Name")); } name. Value = value; If (Property. Changed != null) { Property. Changed(this, new Property. Changed. Event. Args("Name")); } } § 35 Windows Phone
Первичный ключ [Column(Is. Primary. Key = true, Is. Db. Generated = true)] public int Customer. ID { get; set; } § § 38 Windows Phone
Создание класса Sales. DB public class Sales. DB : Data. Context { public Table<Customer> Customers; public Sales. DB(string connection) : base(connection) { } } § § 40 Windows Phone
Создание тестовой базы данных public static void Make. Test. DB(string connection) { Sales. DB new. DB = new Sales. DB(connection); if (new. DB. Database. Exists()) { new. DB. Delete. Database(); } // записать необходимые данные new. DB. Submit. Changes(); } § 42 Windows Phone
Создание тестовых данных string[] first. Names = new string[] { "Rob", "Jim", "Joe", "Nigel", "Sally", "Tim" }; string[] lasts. Names = new string[] { "Smith", "Jones", "Bloggs", "Miles", "Wilkinson" }; foreach (string last. Name in lasts. Names) { foreach (string firstname in first. Names) { // создание записи о клиенте } } § 43 Windows Phone
Создание записи о клиенте // создание записи о клиенте string name = firstname + " " + last. Name; string address = name + "'s address"; string bank = name + "'s bank"; Customer new. Customer = new Customer(); new. Customer. Name = name; new. Customer. Address = address; new. Customer. Bank. Details = bank; new. DB. Customer. Table. Insert. On. Submit(new. Customer); § 44 Windows Phone
Переменные базы данных #region Sales Manager variables public Sales. DB Active. DB; public Customer Active. Customer; #endregion § § 45 Windows Phone
Настройка приложения public App() { // создание тестовой базы данных Sales. DB. Make. Test. DB( "Data Source=isostore: /Sample. sdf"); // подключение к базе данных Active. DB = new Sales. DB( "Data Source=isostore: /Sample. sdf"); } § 46 Windows Phone
Создание запроса LINQ var customers = from Customer customer in this. App. Active. DB. Customer. Table select customer; § § § 47 Windows Phone
Создание запроса LINQ var customers = from Customer customer in this. App. Active. DB. Customer. Table select customer; § § § 48 Windows Phone
Вывод результатов на экран customer. List. Items. Source = customers; § § § 49 Windows Phone
Добавление условий выборки var customers = from Customer customer in this. App. Active. DB. Customer. Table where customer. Name. Starts. With("S") select customer; § § 50 Windows Phone
Структура базы данных Customer. ID Order ID Name Address Bank. Details 123456 Rob 18 Pussycat Mews Nut East Bank 654322 Jim 10 Motor Drive Big Fall Bank 111111 Ethel 4 Funny Address Strange bank Customer. ID Product. ID 1 2 3 123456 111111 654322 1001 1002 1003 Product. ID 1001 1002 1003 Quantity Order. Date Status 1 21/10/2010 Shipped 5 10/10/2010 Shipped 4 01/09/2010 On order Product. Name Windows Phone 7 Cheese grater Boat hook Supplier Microsoft Cheese Industries John’s Dockyard Price 200 2 20 § 54 Windows Phone
Создание связей в классах public class Order { public Date. Time Order. Date; public int Quantity; public Customer Order. Customer; public Product Order. Product; } § § 55 Windows Phone
Связь строк таблиц Customer ID Order ID Name Address Bank Details 123456 Rob 18 Pussycat Mews Nut East Bank 654322 Jim 10 Motor Drive Big Fall Bank 111111 Ethel 4 Funny Address Strange bank Customer ID Product ID 1 2 3 123456 111111 654322 1001 1002 1003 Product ID 1001 1002 1003 Quantity Order Date Status 1 21/10/2010 Shipped 5 10/10/2010 Shipped 4 01/09/2010 On order Product Name Windows Phone 7 Cheese grater Boat hook Supplier Microsoft Cheese Industries John’s Dockyard Price 200 2 20 § 56 Windows Phone
Связи по первичному ключу Customer ID Order ID Name Address Bank Details 123456 Rob 18 Pussycat Mews Nut East Bank 654322 Jim 10 Motor Drive Big Fall Bank 111111 Ethel 4 Funny Address Strange bank Customer ID Product ID 1 2 3 123456 111111 654322 1001 1002 1003 Product ID 1001 1002 1003 Quantity Order Date Status 1 21/10/2010 Shipped 5 10/10/2010 Shipped 4 01/09/2010 On order Product Name Windows Phone 7 Cheese grater Boat hook Supplier Microsoft Cheese Industries John’s Dockyard Price 200 2 20 § 57 Windows Phone
Класс Entity. Ref private Entity. Ref<Customer> order. Customer; [Association(Is. Foreign. Key = true, Storage = "order. Customer")] public Customer Order. Customer { get { return order. Customer. Entity; } set { order. Customer. Entity = value; } } § 59 Windows Phone
Класс Entity. Ref private Entity. Ref<Customer> order. Customer; [Association(Is. Foreign. Key = true, Storage = "order. Customer")] public Customer Order. Customer { get { return order. Customer. Entity; } set { order. Customer. Entity = value; } } § 60 Windows Phone
Атрибут Association private Entity. Ref<Customer> order. Customer; [Association(Is. Foreign. Key = true, Storage = "order. Customer")] public Customer Order. Customer { get { return order. Customer. Entity; } set { order. Customer. Entity = value; } } § 61 Windows Phone
Атрибут Association private Entity. Ref<Customer> order. Customer; [Association(Is. Foreign. Key = true, Storage = "order. Customer")] public Customer Order. Customer { get { return order. Customer. Entity; } set { order. Customer. Entity = value; } } § 62 Windows Phone
Атрибут Association private Entity. Ref<Customer> order. Customer; [Association(Is. Foreign. Key = true, Storage = "order. Customer")] public Customer Order. Customer { get { return order. Customer. Entity; } set { order. Customer. Entity = value; } } § 63 Windows Phone
Ссылка на экземпляр класса private Entity. Ref<Customer> order. Customer; [Association(Is. Foreign. Key = true, Storage = "order. Customer")] public Customer Order. Customer { get { return order. Customer. Entity; } set { order. Customer. Entity = value; } } § 64 Windows Phone
События изменения данных Notify. Property. Changing("Order. Customer"); order. Customer. Entity = value; Notify. Property. Changed("Order. Customer"); § § 65 Windows Phone
Использование ссылки Entity. Ref Customer new. Customer = new Customer(); Order new. Order = new Order(); new. Order. Customer = new. Customer; § § 66 Windows Phone
Класс Entity. Set private Entity. Set<Order> orders = new Entity. Set<Order>(); [Association(Storage="orders", This. Key="Customer. ID", Other. Key="Order. Customer. ID")] public Entity. Set<Order> Orders { get { return orders; } set { orders = value; } } § 68 Windows Phone
Класс Entity. Set private Entity. Set<Order> orders = new Entity. Set<Order>(); [Association(Storage="orders", This. Key="Customer. ID", Other. Key="Order. Customer. ID")] public Entity. Set<Order> Orders { get { return orders; } set { orders = value; } } § 69 Windows Phone
Атрибут Association private Entity. Set<Order> orders = new Entity. Set<Order>(); [Association(Storage="orders", This. Key="Customer. ID", Other. Key="Order. Customer. ID")] public Entity. Set<Order> Orders { get { return orders; } set { orders = value; } } § 70 Windows Phone
Атрибут Association private Entity. Set<Order> orders = new Entity. Set<Order>(); [Association(Storage="orders", This. Key="Customer. ID", Other. Key="Order. Customer. ID")] public Entity. Set<Order> Orders { get { return orders; } set { orders = value; } } § 71 Windows Phone
Атрибут Association private Entity. Set<Order> orders = new Entity. Set<Order>(); [Association(Storage="orders", This. Key="Customer. ID", Other. Key="Order. Customer. ID")] public Entity. Set<Order> Orders { get { return orders; } set { orders = value; } } § 72 Windows Phone
Хранение ссылок [Column] public int Order. Customer. ID; private Entity. Ref<Customer> order. Customer = new Entity. Ref<Customer>(); § § 74 Windows Phone
Описание ассоциации [Association(Is. Foreign. Key = true, Storage = "order. Customer", This. Key = "Order. Customer. ID")] § § 75 Windows Phone
Описание ассоциации [Association(Is. Foreign. Key = true, Storage = "order. Customer", This. Key = "Order. Customer. ID")] § § 76 Windows Phone
Описание ассоциации [Association(Is. Foreign. Key = true, Storage = "order. Customer", This. Key = "Order. Customer. ID")] § § 77 Windows Phone
Ассоциация в классе Order public Customer Order. Customer { get { return order. Customer. Entity; } set { order. Customer. Entity = value; if (value != null) Order. Customer. ID = value. Customer. ID; } } § § 78 Windows Phone
Уведомления LINQ set { Notify. Property. Changing("Order. Customer"); order. Customer. Entity = value; Notify. Property. Changed("Order. Customer"); if (value != null) Order. Customer. ID = value. Customer. ID; } § § 79 Windows Phone
Использование класса Entity. Row Customer new. Customer = new Customer(); Order new. Order = new Order(); new. Order. Customer = new. Customer; new. Customer. Orders. Add(new. Order); § § 80 Windows Phone
LINQ и классы foreach (Order o in active. Customer. Orders) { // выполнить действия с заказом } § 81 Windows Phone
Добавление таблицы Order. Item. ID Order. ID Product. ID Quantity 1 56 1001 1 2 56 1002 5 3 12343 1003 4 § 83 Windows Phone
Запросы LINQ § § § 85 Windows Phone
Простой запрос к базе данных Date. Time search. Date = new Date. Time(2011, 8, 19); var orders = from Order order in active. DB. Order. Table where order. Order. Date == search. Date select order; § § § 86 Windows Phone
Обработка результатов запроса int total. Sales = 0; foreach (Order order in orders) { foreach (Order. Item in order. Order. Items) { total. Sales += item. Order. Item. Product. Price; } } § 87 Windows Phone
Кэширование результатов List<Order> Date. Orders = orders. To. List<Order>(); § § 89 Windows Phone
Ключевое слово join var all. Orders = from Customer c in new. DB. Customer. Table join Order o in new. DB. Order. Table on c. Customer. ID equals o. Order. Customer. ID select new { c. Name, o. Order. Description }; § 91 Windows Phone
Использование результатов List<String> Order. Descriptions = new List<String>(); foreach (var order. Desc in all. Orders) { Order. Descriptions. Add(order. Desc. Name + " заказал: " + order. Desc. Order. Description); } § 92 Windows Phone
Создание нового класса var all. Orders = from Customer c in new. DB. Customer. Table join Order o in new. DB. Order. Table on c. Customer. ID equals o. Order. Customer. ID select new { c. Name, o. Order. Description }; § § 93 Windows Phone
Удаление элементов из таблицы Active. DB. Order. Item. Table. Delete. On. Submit(item); § § 94 Windows Phone
- Slides: 96