class Alumno { public int Id { get; set; } public string Nombre { get; set; } public string DNI { get; set; } public string Email { get; set; } public Alumno(int id, string nombre, string dni, string email) { Id = id; Nombre = nombre; DNI = dni; Email = email; } public override string ToString() { return Id+" - "+Nombre+" - "+DNI+" - "+Email; } }
internal class DAOAlumno { public DAOAlumno() { } public List<Alumno> getAll() { var connection = new SqlConnection(); connection.ConnectionString = "Data Source=.\\SQLEXPRESS;" + "Initial Catalog=pasiona;Integrated Security=True;" + "TrustServerCertificate=True"; connection.Open(); String sql = "select * from alumno"; SqlCommand lectura = new SqlCommand(sql, connection); SqlDataReader sqlDataReader = lectura.ExecuteReader(); List<Alumno> alumnos=new List<Alumno>(); while (sqlDataReader.Read()) { Alumno temp = new Alumno(sqlDataReader.GetInt32(0), sqlDataReader.GetString(1), sqlDataReader.GetString(2), sqlDataReader.GetString(3)); alumnos.Add(temp); } sqlDataReader.Close(); connection.Close(); return alumnos; } }
DAOAlumno dao=new DAOAlumno(); List<Alumno> clase = dao.getAll(); Console.WriteLine(String.Join(" | ",clase));