In this blog, we learn how to make UI and add a lite database that perform insert update or delete operation.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using SQLite;
  7. using sqliteapp;
  8. using sqliteapp.ViewModels;
  9. using sqliteapp.Models;
  10. using Windows.UI.Xaml;
  11. using System.Threading.Tasks;
  12. namespace sqliteapp.ViewModels
  13. {
  14. public class CusotmerViewModel
  15. {
  16. public event PropertyChangedEventHandler PropertyChange;
  17. protected virtual void RaisePropertyChanged(string propertyName)
  18. {
  19. var handler = this.PropertyChange;
  20. if (handler != null)
  21. {
  22. handler(this, new PropertyChangedEventArgs(propertyName));
  23. }
  24. }
  25. //============================================
  26. private int id = 0;
  27. public int Id
  28. {
  29. get { return id; }
  30. set
  31. {
  32. if (id == value)
  33. {
  34. return;
  35. }
  36. id = value;
  37. RaisePropertyChanged("Id");
  38. }
  39. }
  40. private string name = string.Empty;
  41. public string Name
  42. {
  43. get { return name; }
  44. set {if(name == value)
  45. {return;}
  46. name = value;
  47. RaisePropertyChanged("Name");
  48. }
  49. }
  50. private string city = string.Empty;
  51. public string City
  52. {
  53. get { return city; }
  54. set
  55. {
  56. if(city == value)
  57. {return;}
  58. city = value;
  59. RaisePropertyChanged("City");
  60. }
  61. }
  62. private string contact = string.Empty;
  63. public string Contact
  64. {
  65. get { return contact; }
  66. set
  67. {
  68. if(contact == value)
  69. {return; }
  70. contact = value;
  71. RaisePropertyChanged("Contact");
  72. }
  73. }
  74. private sqliteapp.App app = (Application.Current as App);
  75. public CusotmerViewModel GetCustomer(int customerId)
  76. {
  77. var customer = new CusotmerViewModel();
  78. using (var db=new SQLiteConnection(app.dbPath))
  79. {
  80. var _customer = (db.Table<Customer>().Where(
  81. c => c.Id == customerId)).AsEnumerable();
  82. foreach (var a in _customer)
  83. {
  84. customer.Id = a.Id;
  85. customer.Name = a.Name;
  86. customer.City = a.City;
  87. customer.Contact = a.Contact;
  88. }
  89. }
  90. return customer;
  91. }
  92. public CusotmerViewModel GetAllcustomer()
  93. {
  94. var customer = new CusotmerViewModel();
  95. using (var db= new SQLiteConnection(app.dbPath))
  96. {
  97. var result = (from t in db.Table<Customer>()
  98. select t
  99. ).ToList();
  100. if (result != null)
  101. {
  102. foreach (var customer1 in result)
  103. {
  104. customer.Id = customer1.Id;
  105. customer.Name = customer1.Name;
  106. customer.City = customer1.City;
  107. customer.Contact = customer1.Contact;
  108. }
  109. }
  110. }
  111. return customer;
  112. }
  113. public string SaveCustomer(CusotmerViewModel cusotmer)
  114. {
  115. string result = string.Empty;
  116. using (var db=new SQLiteConnection(app.dbPath))
  117. {
  118. string change = string.Empty;
  119. try
  120. {
  121. var existingCustomer = (db.Table<Customer>().Where(c => c.Id == cusotmer.Id)).SingleOrDefault();
  122. if (existingCustomer != null)
  123. {
  124. existingCustomer.Name = cusotmer.Name;
  125. existingCustomer.City = cusotmer.City;
  126. existingCustomer.Contact = cusotmer.Contact;
  127. int success = db.Update(existingCustomer);
  128. }
  129. else
  130. {
  131. int success = db.Insert(new Customer()
  132. {
  133. Id = cusotmer.id,
  134. Name = cusotmer.name,
  135. City = cusotmer.city,
  136. Contact = cusotmer.contact
  137. });
  138. }
  139. result = "Success";
  140. }
  141. catch (Exception ea)
  142. {
  143. result = "This customer was not saved";
  144. }
  145. return result;
  146. }
  147. }
  148. public string DeleteCustomer(int customerId)
  149. {
  150. string result = string.Empty;
  151. using (var db= new SQLite.SQLiteConnection(app.dbPath))
  152. {
  153. var projects = db.Table<Customer>().Where(
  154. p => p.Id == customerId);
  155. foreach (var project in projects)
  156. {
  157. db.Delete(project);
  158. }
  159. var existingCustomer = (db.Table<Customer>().Where(
  160. c => c.Id == customerId)).Single();
  161. if (db.Delete(existingCustomer) > 0)
  162. {
  163. result = "Success";
  164. }
  165. else
  166. {
  167. result = "This customer was not removed";
  168. }
  169. }
  170. return result;
  171. }
  172. }
  173. }