Datareader
The DataReader is used to read data from the database and is oriented only to read and redirect architecture when retrieving data from the database. DataReader will receive data very quickly compared to a data set. Usually we will use the ExecuteReader object to bind data to the datareader.
To bind DataReader data to a GridView, we need to write code, as shown below:
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();
Dataset
A DataSet is an unrelated oriented architecture, which means that when working with datasets there is no need for active connections, and this is a collection of DataTables and relationships between tables. It is used to store several data tables. You can select data form tables, create views based on the table, and set child lines above relationships. The DataSet also provides you with rich features such as saving XML data and loading XML data.
SqlConnection conn = 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); DataSet ds = new DataSet(); da.Fill(ds); gvUserInfo.DataSource = ds; gvUserInfo.DataBind();
Hemlata Gehlot May 30 '17 at 5:41 a.m. 2017-05-30 05:41
source share