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