Hi.....
I know Dataset works in disconnected mode and Datareader works in connected mode.
Datareader is forward only, what is the meaning of forward only. So I want to know difference between Dataset and Datareader in details with an example ?
Thanks......
Loading

Satyapriya NayakPosted Jan 25, 2012, 12:50 PM
A DataReader works in a connected environment, whereas DataSet works in a disconnected environment.
A DataReader object represents a forward only, read only access to data from a source. It implements IDataReader & IDataRecord interfaces. For example, The SQLDataReader class can read rows from tables in a SQL Server data source. It is returned by the ExecuteReader method of the SQLCommand class, typically as a result of a SQL Select statement. The DataReader class' HasRows property can be called to determine whether the DataReader retrieved any rows from the source. This can be used before using the Read method to check whether any data has been retrieved.
Example
Dim objCmd as New SqlCommand("Select * from t_Employees", objCon)
objCon.Open()
Dim objReader as SqlDataReader
objReader = objCom.ExecuteReader(CommandBehavior.CloseConnection)
If objReader.HasRows = True then
Do While objReader.Read()
ListBox1.Items.Add(objReader.GetString(0) & vbTab & objReader.GetInt16(1))
Loop
End If
objReader.Close()
A DataSet represents an in-memory cache of data consisting of any number of inter-related DataTable objects. A DataTable object represents a tabular block of in-memory data. Further, a DataRow represents a single row of a DataTable object. A Dataset is like a mini-database engine, but its data is stored in the memory. To query the data in a DataSet, we can use a DataView object.
Example
Dim objCon as SqlConnection = New SqlConnection("server=(local);database=NameOfYourDb;user id=sa; password=;)
Dim da as New SqlDataAdapter
Dim ds as DataSet = New DataSet
da.SelectCommand.Connection = objCon 'The Data Adapter manages on its own, opening & closing of connection object
da.SelectCommand.CommandText = "Select * from t_SomeTable"
da.Fill(ds,"YourTableName")
Suppose you want to bind the data in this dataset to a gridview
Gridview1.DataSource = ds
Gridview1.DataMember = "TableName"
Gridview1.Databind()
Thanks
Vineet Kumar SainiPosted Jan 30, 2012, 11:16 AM
Krishna GaradPosted Jan 26, 2012, 7:31 AM
Why we say datareader is forward only because it reads the data in forward manner consider you read the data like bellow
string column1=dr["Column1"].ToString()
string column2=dr["Column2"].ToString()
for above case we did not specified any row no. because datareader can not switch between rows but consider this in case of dataset like bellow.
string val1=ds.Tables[0].Rows[0][0].ToString()
in dataset we can switch between the rows but in datareader we don't have any rows like ds.Table.rows. that's we say datareader is forward only because once we read the row then if we want read same row then we have to finish the reading of remaining row in result set and then again read it from start of resultset.
VulpesPosted Jan 25, 2012, 11:05 AM
http://www.c-sharpcorner.com/uploadfile/61b832/datareader-vs-dataset/