How to read the LAST record of datareader?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    How to read the LAST record of datareader?

    Hi

    How to read the LAST record of datareader? Here is my code

    [code

    StringBuilder objBuilder = new StringBuilder()
    objBuilder.Appe nd("SELECT startdate, enddate FROM records where userid = '")
    objBuilder.Appe nd(id)
    objBuilder.Appe nd("'")

    SqlDataReader dr = null
    int RecordCount = 0

    tr

    dr = SqlHelper.Execu teReader(DBConn ection.ConnStri ng, CommandType.Tex t, objBuilder.ToSt ring())

    while (dr.Read()

    RecordCount++


    if(dr.) // how to read the LAST record of the selected rows

    this.StartDate = Convert.ToDateT ime(dr["startdate"])
    this.EndDate = Convert.ToDateT ime(dr["enddate"])



    [/code

    I know how to count the total number of row. But, I only need the last row of data. dr.Read() gets the first row only. How can I do it

    Thanks for hel

  • Jon Skeet [C# MVP]

    #2
    Re: How to read the LAST record of datareader?

    Tom <kerocow@yahoo. com> wrote:[color=blue]
    > I know how to count the total number of row. But, I only need the
    > last row of data. dr.Read() gets the first row only. How can I do it?[/color]

    I would suggest reversing the order of your query (which is currently
    unordered as far as I can see - selecting just the last result could
    give you a different record each time) and using SELECT TOP 1. Then you
    know you'll only get one record.

    I'd also highly recommend using a parameter instead of adding a string
    directly into the SQL.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...