R
Rajesh Patel
check recordsaffected() property which returns integer. may be that helps
you.
Rajesh Patel
you.
Rajesh Patel
Anders Borum said:Hello!
I realized there was another way to get around the problem. If you're facing
recursion in your code, it's harder to check, whether a nested recursion has
advanced the SqlDataReader beyond it's last record.
So, I simply decided to check on the IsClosed property on the SqlDataReader.
You can programatically force a SqlDataReader to close, simply by calling
the method Close().
// Advance SqlDataReader to next record (if available)
if (sqlReader.Read() == false)
{
// Force SqlDataReader to close
sqlReader.Close()
}
.. and then, in the parent method, you simply do the following:
if (sqlReader.IsClosed)
{
break;
}
Here's a small sample I wrote to document the idea. I'd still like to have a
property, that allows you to check for additional records.
void Recurse(SqlDataReader sqlReader)
{
if (sqlReader.Read() == false)
{
sqlReader.Close();
return;
}
int m_Right = (int) sqlReader["Right"];
while (while((int) sqlReader["Right"] < m_Right))
{
Recurse(sqlReader);
if (sqlReader.IsClosed)
{
break;
}
}
}
--
venlig hilsen / with regards
anders borum
--
leftWilliam Ryan said:Rajesh:
With all due respect, I think you are mixing things up:
<<however, datareader.nextresult doesn't show that you have more rows or not
in the current dataset. it just returns is there any more resultsetandto
read or not.>>
Technically, you are right b/c NextResult only applies to a DataReaderneeda
DataReader is a connected object, not a disconnected one. You don'tscrappinga
DataSet to use a DataReader, and normally you would never use both objects
in for the same task. They are intended for totally different purposes.
NextResult returns a boolean value, if NextResult returns false, you are at
the end (or possibly at the beginning of a DataReader that doesn't have any
rows - in which case you can use DataReader.HasRows if you have VS2003).
One of the primary reasons one would choose .NextResult vs. While dr.Read()
is to determine such a thing.
While Dr.Read would only know you hit the end After you hit it, NextResult
would return False as soon as you were at the last record.
If there aren't any results to return , then you are at the end of the set.
As far as writing logic goes, if you are using a DataReader, you Have to
iterate through it to determine if you are at the end. The only exception
would be if you didn't have any rows, but if you verify that you have rows,
then you need to walk the Reader in order to find out when you are at the
end.
Using a DataSet in many instances is totally overkill b/c of the overhead
associated with the DataAdapter (assuming you are using a DataSet/DataTable
to retrieve data).
I don't mean to parse words, but it's important to draw a distinction
between connected and disconnected objects b/c the behave Totally
differently and performance and functionality is night and day depending on
the task.
Also, you don't need a DataSet at all if you are working with one query.
You can simply use a DataTable, and check its Rows.Count property,
Rows.Count-1 would give you the last record, but there are a lot of things
(mainly performance and needed functionality) to consider beforea
DataReader based approach in favor of a disconnected methodology.
HTH,
Bill
left
to are if
you records
the if would
be Read()