Guest User

Untitled

a guest
Jun 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. // open your connection / datareader etc.
  2.  
  3. List<Customer> customers = new List<Customer>();
  4.  
  5. while(dataReader.Read())
  6. {
  7. Customer c = new Customer();
  8. c.Id = dataReader.GetInt32(0);
  9. c.Name = dataReader.GetString(1);
  10. // etc (you might want to use string indexers instead of ints for the get methods)
  11.  
  12. customers.Add(c);
  13. }
  14.  
  15. // close and dispose your datareader / connection etc as usual
  16.  
  17. return View("List", customers);
  18.  
  19. private DataTable GetData()
  20. {
  21. DataTable dt = new DataTable();
  22.  
  23. using (SqlConnection connection
  24. = new SqlConnection("ConnectionString"))
  25. using (SqlCommand command = new SqlCommand())
  26. {
  27. command.Connection = connection;
  28. command.CommandText = "SELECT * FROM Customers";
  29.  
  30. connection.Open();
  31. using (SqlDataReader reader =
  32. command.ExecuteReader
  33. (CommandBehavior.CloseConnection))
  34. {
  35. dt.Load(reader);
  36. }
  37. }
  38.  
  39. return dt;
  40. }
Add Comment
Please, Sign In to add comment