Find last row in a database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Search thru all my books and MSDN database I can't seem to find a easy
solution to my problem.
What I would like to know if there is a fast and easy way to get the the
amount of rows that are in a Table in a Database. Without needing to load
the table into a DataTable / DataSet or needing to use the datareader.read
until it hits the last row ?

I am using OleDB.

Bardo
 
Search thru all my books and MSDN database I can't seem to find a
easy solution to my problem. What I would like to know if there is a
fast and easy way to get the the amount of rows that are in a Table in
a Database. Without needing to load the table into a DataTable /
DataSet or needing to use the datareader.read until it hits the last
row ?

I am using OleDB.

Bardo

What your subject is asking would be something like so:

SELECT TOP 1 * FROM Table ORDER BY SomeFieldDefiningOrder DESC

What you ask in the body would be something like:

SELECT COUNT(*) FROM Table

From the ado.net side, there isn't a way to determine how many rows were
selected, but if you execute a count query before your main query, you could
get that value from the first recordset.
 
thanks,
with Select Count(*) from TABLENAME I got it working.

I just have one more question.
How can I retreive the data from the last row , now that I know how many
rows there are?

Bardo
 
It depends on your Tables sorting. Lets assume that your records sorted by
their CustID nad your select statement is
SELECT * from Customers
You want to access the last record. Than what you should do is
SELECT TOP 1 * from Customers Order By CustId DESC
then last column happened the First ! then you take it
 
Back
Top