Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // open your connection / datareader etc.
- List<Customer> customers = new List<Customer>();
- while(dataReader.Read())
- {
- Customer c = new Customer();
- c.Id = dataReader.GetInt32(0);
- c.Name = dataReader.GetString(1);
- // etc (you might want to use string indexers instead of ints for the get methods)
- customers.Add(c);
- }
- // close and dispose your datareader / connection etc as usual
- return View("List", customers);
- private DataTable GetData()
- {
- DataTable dt = new DataTable();
- using (SqlConnection connection
- = new SqlConnection("ConnectionString"))
- using (SqlCommand command = new SqlCommand())
- {
- command.Connection = connection;
- command.CommandText = "SELECT * FROM Customers";
- connection.Open();
- using (SqlDataReader reader =
- command.ExecuteReader
- (CommandBehavior.CloseConnection))
- {
- dt.Load(reader);
- }
- }
- return dt;
- }
Add Comment
Please, Sign In to add comment