Click here to Skip to main content
15,891,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I want to find last record from odbcdatareader.I tried to find movelast method but failed to find.Please suggest me solution.

Here is code:
I want same code for MoveLast and MoveFirst.

C#
public bool MoveNext(Object reader)
       {
           OdbcDataReader rs = null;
           rs = (OdbcDataReader)reader;
           //Check if OdbcDataReader is valid or not
           if (rs != null)
           {

               try
               {
                   //Move to next record
                   return (rs.Read());

               }
               catch (Exception Ex)
               {
                   return false;
               }
           }
           return false;
       }
Posted
Updated 14-Mar-12 19:01pm
v2

1 solution

First of all, this is not how data reader works. The return of the method Read is just the Boolean value used to stop reading. The problem is: you don't explain what is the "record" to return. There is no a data type which represents a "record" in your code, except the reader itself. All you can do is getting the data from the reader at some position (row of the data set produced by a query); you can use Item properties or the method GetData for every column, where the number of if found by reading the property FieldCount.

You can create some data container to hold a copy of the record data, but why doing all that? You can use the data types System.Data.DataTable or System.Data.TypedDataTable instead, please see:
http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx[^],
http://msdn.microsoft.com/en-us/library/bb358258.aspx[^].

Also, you don't have a random access to the records. You can read forward-only. If you need the first record, you should remember some data after the very first read, that's it. The methods like "MoveFirst" or "MoveLast" would not make sense.

Please see:
http://msdn.microsoft.com/en-us/library/system.data.common.dbdatareader.aspx[^].

More importantly, the notion of "last" record also makes no much sense. Formally, you can read using the data reader to the very end and them use the data reader instance to get the record information. Call it "last", but it is last only formally, only for a given case. The database itself does not support any specific order of the records in the table, and the "table" retrieved by a data reader does not physically exists — it's an artifact of the query. You can control the order by the SQL clause "ORDER BY", but if you don't, the order of data which comes with data reader is undefined. Are you sure you really need "that" last record.

I would recommend to learn better some more basic ideas on how ADO.NET works. This is a very good CodeProject article:
Using ADO.NET for beginners[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900