Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. public class User
  2. {
  3. public int UserID { get; set; }
  4. public string FirstName {get; set;}
  5. public string LastName {get;set;}
  6. public bool? Active {get;set;}
  7. public int? Age { get; set; }
  8. public string Sex { get; set; }
  9.  
  10. }
  11.  
  12. // Initialize just two fields and leave the other to their defaults
  13. // (null for both strings and nullable ints)
  14. User u = new User();
  15. u.UserID = 1;
  16. u.FirstName = "Steve";
  17. bool ok = UpdateUser(u);
  18. if(ok) ......
  19.  
  20.  
  21. public UpdateUser(User info)
  22. {
  23. using(SqlConnection cnn = new SqlConnection(@"Data Source=(LOCAL);
  24. Initial Catalog=TestDB;
  25. Integrated Security=True;"))
  26. {
  27. cnn.Open();
  28.  
  29. // Prepare the parameters to pass to the Dapper Execute
  30. var pms = new
  31. {
  32. UserID = info.UserID
  33. FirstName = info.FirstName,
  34. LastName = info.LastName, // <- from here all is null
  35. Active = info.Active,
  36. Age = info.Age,
  37. Sex = info.Sex
  38. };
  39.  
  40. int rows = cnn.Execute(@"UPDATE [UserTable]
  41. SET FirstName= @FirstName,
  42. LastName = @LastName,
  43. Active = @Active,
  44. Age = @Age,
  45. Sex = @Sex
  46. WHERE UserID = @UserID",
  47. pms);
  48. return rows != 0;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement