Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using ServiceStack;
  4. using ServiceStack.Text;
  5. using ServiceStack.OrmLite;
  6. using ServiceStack.OrmLite.Sqlite;
  7. using ServiceStack.DataAnnotations;
  8.  
  9. /*** ASYNC in OrmLite
  10. * Essentially all OrmLite public API's have async equivalents of the same
  11. * name with an additional conventional `*Async` suffix. The Async API's also
  12. * take an optional CancellationToken making converting sync code trivial,
  13. * where you just add the `*Async` suffix and `await` keyword as seen below:
  14. ***/
  15.  
  16. public class User
  17. {
  18. public long Id { get; set; }
  19.  
  20. [Index]
  21. public string Name { get; set; }
  22. public DateTime CreatedDate { get; set; }
  23. public override string ToString() => Name;
  24. }
  25.  
  26. var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
  27. var db = dbFactory.Open(); //Open ADO.NET DB Connection
  28.  
  29. db.DropAndCreateTable<User>(); //DROP (if exist) and CREATE Table from User POCO
  30.  
  31. await db.InsertAsync( //INSERT multiple Users by params
  32. new User { Id = 1, Name = "A", CreatedDate = DateTime.Now },
  33. new User { Id = 2, Name = "B", CreatedDate = DateTime.Now },
  34. new User { Id = 3, Name = "C", CreatedDate = DateTime.Now },
  35. new User { Id = 4, Name = "C", CreatedDate = DateTime.Now });
  36.  
  37. var rowsC = await db.SelectAsync<User>(x => x.Name == "C"); //SELECT by typed expression
  38. "No of 'C' Rows: {0}, Ids:".Print(rowsC.Count); //= 2
  39. rowsC.ConvertAll(x => x.Id).PrintDump(); //= 3,4
  40.  
  41. await db.DeleteAsync<User>(x => x.Name == "C"); //DELETE by typed expression
  42.  
  43. var remainingC = await db.SelectAsync<User>("Name= @name", new { name="C" }); //Custom SQL
  44. "No of 'C' Rows: {0}".Print(remainingC.Count); //= 0
  45.  
  46. var rowsLeft = await db.SelectAsync<User>();
  47. "Rows Left: {0}".Print(rowsLeft.Count); //= 2
  48. rowsLeft.PrintDump(); //= A,B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement