2012-10-10 28 views
-3

可能重複:
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; 
} 

在上面的代碼我寫使用數據適配器與其它兩種方式(各個評價部內)檢索數據的,一個使用datareader。哪一個會跑得更快?

+14

爲什麼不嘗試一下自己之前加載? – sloth

+2

您是否對您的樣本數據集進行了測試? 'System.Diagnostics.Stopwatch'是這些類型問題的朋友。 – jheddings

+0

是什麼讓你覺得這兩種選擇之間還有區別? –

回答

3

DataReader是最快的方法。 在任何情況下更快然後DataAdapter的DataAdapter的作爲內部使用和的DataReader執行其他操作(與數據集多個表的作品,......)

DbDataAdapter - FillInternalMethod

+1

考慮到查詢的簡單性,你如何能做出這個聲明? –

1

如果執行select operations我suggets您使用SqlDataReader

只有在內存一條記錄時間,而不是整個結果集

速度大約是它,你可以得到一個迭代

允許你開始處理結果越早

諾塔:

隨着SqlDataAdapter的,你有更高的內存使用,你等到所有的數據都使用它的任何