Check if it's the last record in sqldatareader

半世苍凉 提交于 2020-01-12 14:01:21

问题


Is there a way to check if I'm on the last record ? thanks


回答1:


Use this pattern to identify and process the last row in result:

if (reader.Read())
{
    var loop = true;
    while (loop)
    {
        //1. Here retrive values you need e.g. var myvar = reader.GetBoolean(0);
        loop = reader.Read();
        if (!loop)
        {
            //You are on the last record. Use values read in 1.
            //Do some exceptions
        }
        else {
            //You are not on the last record.
            //Process values read in 1., e.g. myvar
        }
    }
}



回答2:


Other than "there isn't another one afterwards", no. This may mean you need to buffer the row, try to read, then look at the buffered data if it turned out to be the last.

In many cases, when the data is moderate and you are building an object model of some kind, you can just looks at the constructed data, i.e. make a List<T> of all the rows when using the reader, then after that, look at the last row in the list.




回答3:


Another possibility would be to put the information into the data set.

You are reading the data using a query. Let's call it q. Use this query instead:

select row_number() over (order by (select NULL)) as seqnum,
       count(*) over (partition by null) as numrecs
       q.*,
from (<q>) q
order by seqnum

Now the last record has seqnum = numrecs, which you can test for in your application.

By the way, this assumes that you are retrieving the data in an unordered way. If you want ordering, put the criteria in the "order by" clause for row_number().




回答4:


To iterate through the SqlDataReader:

SqlDataReader reader = ...;
while(reader.Read())
{
    // the reader will contain the current row
}

There's nothing in the reader telling you it's the last row. If you still need to get on the last row, perhaps storing the row's data in a seperate variable you can get that data after exiting the loop.

Other alternatives would be:

  • to get the data into a DataSet/DataTable
  • to store the data in a list/array
  • retrieve the data in reverse order (rewrite your query), so that the first row is the one you want.



回答5:


  1. As you know, a SQLDataReader doesn't have a "row count" property. You just read until EOF.

  2. However, a DataSet does. Perhaps you can substitute a DataAdapter and some kind of data set or table:

    • http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/dc198199-be24-473c-9973-746fe14d65ba/



回答6:


You can't do that without repeating yourself i.e. fetching the record twice. Here is a shorter method

System.Data.DataTable dt = new System.Data.DataTable();
dt.Load(myDataReader);
int recordCount = dt.Rows.Count;
if(recordCount > 0)
{
   if(dt.Rows[recordCount-1]["username"] == "wrong value")
     throw new Exception("wrong value");
}

I wrote this off-hand so it's not tested but that's the approach



来源:https://stackoverflow.com/questions/11703305/check-if-its-the-last-record-in-sqldatareader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!