Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Development (http://www.go4expert.com/forums/web-development-forum/)
-   -   What is the difference between a DataTable and a DataReader in ASP? (http://www.go4expert.com/forums/difference-datatable-datareader-asp-t28357/)

hireaspdeveloper 10May2012 11:04

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

bzforum 30May2012 12:02

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

Originally Posted by hireaspdeveloper (Post 94849)
What is the difference between a DataTable and a DataReader?

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.

vijaywebsolutions 7Nov2014 12:13

Re: What is the difference between a DataTable and a DataReader in ASP?
 
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


All times are GMT +5.5. The time now is 11:36.