1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What is the difference between a DataTable and a DataReader in ASP?

Discussion in 'Web Development' started by hireaspdeveloper, May 10, 2012.

  1. hireaspdeveloper

    hireaspdeveloper Banned

    Joined:
    Apr 26, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    ASP Developer
    Location:
    Lilburn
    Home Page:
    What is the difference between a DataTable and a DataReader?
     
  2. bzforum

    bzforum New Member

    Joined:
    May 20, 2012
    Messages:
    25
    Likes Received:
    7
    Trophy Points:
    0
    Occupation:
    CEO & Founder
    Location:
    Mumbai , India
    Home Page:
    DataReader fetches one record at a time..when ever you all theRead() function the next record if there is any gets fetched..now for this the connection to the database has to be open when the read function is called..

    on the other Hand

    DataTable fetches all the records when you call the Fill() function of the DataAdapter. It keeps the records in memory so that you can use it latter without been connected to the database..This is an Example of .NET Disconnected Architecture ..

    DataReader is lighter than DataTable.
     
    shabbir likes this.
  3. vijaywebsolutions

    vijaywebsolutions New Member

    Joined:
    Apr 18, 2014
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    You can see the actual difference between DataTable & dataReader below:

    DataTable

    DataTable represents a single table in the database. It has rows and columns. There is no much difference between dataset and datatable, dataset is simply the collection of datatables.

    eg.
    protected void BindGridview()

    {

    SqlConnection con = new SqlConnection("Data Source=abc;Integrated Security=true;Initial Catalog=Test");

    conn.Open();

    SqlCommand cmd = new SqlCommand("Select UserName, First Name,LastName,Location FROM Users", conn);

    SqlDataAdapter sda = new SqlDataAdapter(cmd);

    DataTable dt = new DataTable();

    da.Fill(dt);

    gridview1.DataSource = dt;

    gvidview1.DataBind();

    }

    DataReader

    DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. DataReader will fetch the data very fast when compared with dataset. Generally we will use ExecuteReader object to bind data to datareader.

    To bind DataReader data to GridView we need to write the code like as shown below:


    eg.
    Protected void BindGridview()

    {

    using (SqlConnection conn = new SqlConnection("Data Source=abc;Integrated Security=true;Initial Catalog=Test"))

    {

    con.Open();

    SqlCommand cmd = new SqlCommand("Select UserName, First Name,LastName,Location FROM Users", conn);

    SqlDataReader sdr = cmd.ExecuteReader();

    gvUserInfo.DataSource = sdr;

    gvUserInfo.DataBind();

    conn.Close();

    }

    }

    Holds the connection open until you are finished (don't forget to close it!).
    Can typically only be iterated over once
    Is not as useful for updating back to the database
     
  4. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    3
    Trophy Points:
    0
    Data Reader
    -Its an connection oriented, whenever you want fetch the data from database, you need the connection,after fetch the data connection is disconnected.
    -Its an Read only format, you can't update records.
    -DataReaders provide a very efficient way to access data, and can be thought of as a Firehose cursor from ASP Classic, except that no server-side cursor is used.
    Data Table
    -A DataTable object represents a single table in the database. It has a name rows and columns.
    -The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data.
     
  5. JohnAdams

    JohnAdams New Member

    Joined:
    Oct 15, 2016
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    A DataReader is an object returned from the ExecuteReader method of a DbCommand object. It is a forward-only cursor over the rows in the each result set. Using a DataReader, you can access each column of the result set, read all rows of the set, and advance to the next result set if there are more than one.

    DataTable represents a single table in the database. It has rows and columns. There is no much difference between dataset and datatable, dataset is simply the collection of datatables.
     

Share This Page