Advertisement
Guest User

Simplified SqlReader operations

a guest
Mar 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1. var customers = new Dictionary<string, Customer>();
  2.  
  3. While(sqlReader.Read())
  4. {
  5.     // If you give the Customer object public properties you can initialize
  6.     / like this.  The class also requires an empty constructor.  This is default if you don't add a constructor
  7.     Customer customer = new Customer()
  8.     {
  9.         FirstName = sqlReader["FirstName"]?.ToString() ?? "",
  10.         LastName = sqlReader["LastName"]?.ToString() ?? "",
  11.         Vin = sqlReader["Vin"]?.ToString() ?? "",
  12.     };
  13.  
  14.     customers.Add(customer.Vin.Trim().ToLower(), customer);
  15. }
  16.  
  17. // Customer Class example  (you may already know this)
  18. public class Customer
  19. {
  20.     public string FirstName {get;set;}
  21.     public string LastName {get;set;}
  22.     public string Vin {get;set;}
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement