Sunday, January 13, 2013

DataSet vs DataReader ADO.NET using C#


The DataSet actually uses a DataReader to populate itself. A DataReader is a lean, mean access method that returns results as soon as they’re available, rather than waiting for the whole of the query to be populated into a DataSet. This can boost your application performance quite dramatically, and, once you get used to the methodology, can be quite elegant in itself.
THE ADVANTAGES OF DATAREADER IN ACTION
To highlight the advantages of using a DataReader over the DataSet, here’s an example of using a DataSet. The following fills a DataSet with the results from a table, and outputs the first field in each row:
  SqlConnection conn = new SqlConnection(connectionString); 

  SqlDataAdapter a = new SqlDataAdapter 

 ("select * from mytable;",conn); 

  DataSet s = new DataSet(); 

  a.Fill(s); 

  foreach (DataRow dr in s.Tables[0].Rows) 

  { 

    Console.WriteLine(dr[0].ToString()); 

  }
As you can see, we don’t actually start the actual inspection of data (the foreach loop), until the whole DataSet has been filled. There may be occasions where we may not use all our results, or we might execute other code while inspecting (a progress bar’s progress is a trivial example). Using a DataSet, this can only take place after the complete results are fetched and passed into the various collections within the DataSet.
In contrast, here’s code that achieves the same results using a DataReader in place of a DataSet:
  SqlConnection conn = new SqlConnection(connectionString); 

  SqlCommand comm = new SqlCommand("select * from mytable", conn); 

  comm.Connection.Open(); 

  SqlDataReader r =  

      comm.ExecuteReader(CommandBehavior.CloseConnection); 

  while(r.Read())  

  { 

    Console.WriteLine(r.GetString(0)); 

  } 

  r.Close(); 

  conn.Close();

 

Here, the inspection is made as soon as data is available by employing the while loop, where r.Read()returns false if no more results are found. Not only can we therefore inspect as we go, but the DataReader only stores one result at a time on the client. This results in a significant reduction in memory usage and system resources when compared to the DataSet, where the whole query is stored

No comments:

Post a Comment

Creating mirror of BST