-3

Possible Duplicate:
Is datareader quicker than dataset when populating a datatable?

 public DataTable GetReviewsId(Objects myObjects)
 {
     DataTable tblBindReviews = new DataTable();
     string Query = "";
     try
     {
         Query = "select distinct ProductId from tblReview where ProductId in (select ProductId from tblProduct where R=0 and T=0)";
         /*SqlConnection mySqlConnection = this.SetDatabaseConnection();
         SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Query, mySqlConnection);
         mySqlDataAdapter.Fill(tblBindReviews);*/
         /*mySqlConnection.Open();
         SqlCommand cmd = new SqlCommand(Query,mySqlConnection);
         tblBindReviews.Load(cmd.ExecuteReader());*/
     }
     catch (SqlException ex)
     {
         throw new Exception(ex.Message);
     }
     finally
     {
         this.ClosedatabaseConnection();
     }
     return tblBindReviews;
 }

In the above code i have written two ways(each inside the commented part) of retrieving data, one using data adapter and the other using datareader. which one of the two will run faster?

Community
  • 1
  • 1
Niar
  • 532
  • 2
  • 11
  • 23
  • 14
    Why not try it yourself? – sloth Oct 10 '12 at 13:04
  • 2
    Have you run a test over your sample data set? `System.Diagnostics.Stopwatch` is your friend for these types of questions. – jheddings Oct 10 '12 at 13:05
  • What makes you think there is even a difference between the two options? – Security Hound Oct 10 '12 at 13:06
  • i tried but they seem to give me the same result while debugging using visual studio 2010.. – Niar Oct 10 '12 at 13:08
  • Use a larger data set or run your test inside a loop. If you are concerned about Debug vs Release, try on both. If you get the same or undistinguishable performance, then which one is probably up to you. – jheddings Oct 10 '12 at 13:08
  • @jheddings..can you tell me how i can use the System.Diagnostics.Stopwatch ..can i just include it in my code – Niar Oct 10 '12 at 13:09
  • The docs have a nice example: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx Of course, instead of sleeping, you would access your database. – jheddings Oct 10 '12 at 13:11

2 Answers2

3

DataReader is the fastest way. In any case faster then DataAdapter as DataAdapter uses DataReaders inside and performs additional operations (works with datasets multiple tables, .....)

DbDataAdapter - FillInternalMethod

pero
  • 4,169
  • 26
  • 27
1

If you execute select operations i suggets you to use SqlDataReader

Only has one record in memory at a time rather than an entire result set

Is about as fast as it you can get for that one iteration

Allows you start processing results sooner

Nota :

With SqlDataAdapter you have Much higher memory use, you wait until all the data is loaded before using any of it

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51