Does the ADO.NET DataTable respect the Order By Clause?

0 comments suggest edit

Can somebody out there point me to a reference that explicitly says whether or not we can count on the underlying order of a DataTable rows to be the same order that is returned by a SQL statement or a Stored Procedure?

I read a posting somewhere where the author states that though it appears to be the case that the DataTable rows are ordered in the same order as retrieved from the database, that this ordering is not guaranteed by ADO.NET and should not be relied upon. For those familiar with hash tables, you know that a hashtable gives no guarantees about how elements are sorted.

Now I know all about the DataView class and how that can be used to have a sorted view of items in a DataTable. But my concern is this. Suppose the DataTable does respect ordering (for now) and thus my underlying data is already sorted. If the DataView uses traditional Quicksort to sort the data, that is the pathological worst case. Now there are new variants of quicksort that handle already sorted data just fine. I have yet to run benchmarks to find out how the DataView performs.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.



2 responses

  1. Avatar for Charu Gupta
    Charu Gupta June 5th, 2005

    How is Datatable different from Hashtable?

  2. Avatar for Alison
    Alison May 8th, 2008

    I am actually seeing this issue..
    I have a sql string that I use in my cmd object to fill the datatable using ADO.Net. If I paste the sql into the MS Access query tool, I get the correct order I want.. (no order by just pulled in table order). But when the Fill populates the datatable it is magically ordered in a differnt way.. Not even sure how it is ordered.. I need the rows to be just as they are stored in the DB.. So this is a major problem..
    Anyone run into a solution?