Friday 14 March 2014

Difference between Dataset and Datareader in Asp.net?


Difference between Dataset and Datareader



Dataset
DataSet is a disconnected architecture orient means no need for active connections when working with data sets and is a collection of data tables and relationships between tables. Used to contain multiple tables with data.Dataset also provide rich feature like  saving data XML or loading XML data.

Example :



// This method is used to bind gridview from database
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("select UserName,LastName,Location from UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}


DataReader

DataReader is used to read data from the database and it is a read only and forward connection-oriented architecture for retrieving data from the database. DataReader is used to iterate through results coming from a register server and is read at a time because the memory consumption will be lower and you will get very fast data compared to the data set.Execute Reader object to used to bind data to DataReader.


// This method is used to bind gridview from database
protected void BindGridview()
{
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select UserName,LastName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
gvUserInfo.DataSource = dr;
gvUserInfo.DataBind();
con.Close();
}
}

0 comments:

Post a Comment