Here’s a tricky one… I noticed in a query for my code that when I used the .NET DataAdapter.Fill method as shown below to query an Access database, the order of the records was not the “natural” order of the records (as they were originally inserted into the table).
OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter("SELECT * FROM SomeTable ", oleDbConnection);
oleDbAdapter.Fill(sourceData, "SomeTable" );
foreach(DataRow theRow in sourceData.Tables["SomeTable"].Rows)
{ ... }
On one table, I had a primary key so I was just able to order by the primary key. I have a new table which does not have any primary key and I would like to query the table and have the records ordered by the natural table order. Should I be using a OleDbDataReader to preserve the order, or is there some way to make the OleDbDataAdapter.Fill method to preserve order?
The
Fill()method of aDataAdapteris equivalent toExecuteReader(CommandBehavior.Default)so you will not gain anything when it comes to preserving order by using one method or the other.The
CommandBehaviorenumeration does not, apparently, give any option to specify explicitly that the table should be read in natural order.I find it confusing though that
DataAdapter.Fillshould reorder the natural order of the data stored in the DataBase.EDIT: More on Natural order
Are there any indexes defined in your table? MS Access will show the data in the table ordered by any defined indexes and therefore it will not preserve the natural order when visualizing data (insert order).
On the other hand .Fill() will conserve natural order no matter what indexes are defined in the source table, therefore what you might percieve as not reading in natural order might be due to an index in the source table and not a problem in the
Fill()method.I have done a few quick tests and in all of them
DataAdapteris returning rows preserving natural order.