Lucas Rodr
Lucas Rodr

Reputation: 1

Are Datasets/Datatables the only way to return data?

In a 3 tier architecture (C# , ADO.NET), are datasets and datatables the only option to return data from the Data Layer to the Presentation Layer?. I've been working with Datatables but when I will ttry to do this

public User getUserByP(User user)
    {
        User t = new User();
        using(SqlConnection con = new SqlConnection(Conexion.Cn))
        {
            con.Open();
            SqlCommand command = new SqlCommand("spLogIn_User", con);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@user", User.user);
            command.Parameters.AddWithValue("@password", User.Pass);
            SqlDataReader reader =  command.ExecuteReader();
            while (reader.Read())
            {
                t.IdUser = reader.GetInt32(0);
                t.Name = reader.GetString(1);
                t.LastName = reader.GetString(2);
                t.Access = reader.GetString(3);
                t.user = reader.GetString(4);
                t.Pass = reader.GetString(5);
            }
        }
        return t;
    }

I ill have an error cause there is no communication between Data Layer and Presentation Layer. This is posible in MVC (i think) but no here. So if a just want return even 1 result , datatble or dataset are only option?

Upvotes: 0

Views: 71

Answers (1)

RaulMonteroc
RaulMonteroc

Reputation: 186

No. There are other alternatives to retrieve data from databases into your data layer.

  • Linq to sql
  • Entity Framework
  • nHibernate
  • Dapper.Net

If on the other hand, you want to know how to access the data layer from the presentation layer without additional dependencies, you could do the following.

  1. In the data layer, always return a simple object instead of an specialized type (datatable/dataset) to avoid unnecessary dependencies to system.data or other dlls

  2. Add a business layer. In a 3 tier application, this layer uses the data from the data layer and adds validation, business rules and logic to access and manipulate that data.

  3. Reference the business layer in the presentation layer to display the information desired.

For more details on how to achieve a 3-tier/layer architecture refer to this explanation

Upvotes: 1

Related Questions