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:
    http://www.hireaspdeveloper.com/
    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:
    http://bzforum.in/index.php
    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.
    Code:
    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.
    Code:
    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:
    5
    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 Member

    Joined:
    Oct 15, 2016
    Messages:
    46
    Likes Received:
    2
    Trophy Points:
    8
    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.
     
  6. Aspen Colton

    Aspen Colton Member

    Joined:
    Apr 21, 2016
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    6
  7. Arusi kumari

    Arusi kumari New Member

    Joined:
    Jan 28, 2023
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Records cannot be updated because it is a read-only format. It is connection-oriented; whenever data is retrieved from a database, a connection is required. Since it's in read-only format, we can't update it. Data Reader is used to read data from databases, and its connection-oriented architecture is read-only while fetching data from databases. When compared to a dataset, a data reader will retrieve the data quite quickly. Typically, we'll connect data to a data reader using an Execute Reader object.
    Where as
    Data Table represents a single table in the database It has rows and columns. There is no much difference between dataset and data table, Dataset is simply the collection of data tables. A single database table is represented by a Data Table. It has columns and rows. Datasets and data tables are nearly identical; a dataset is essentially a collection of data tables.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice