I have a select statement that all I want to do is get the number of
rows returned in my query:
string strSQLAccountInfo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUserid.Trim() +
"' ";
SqlCommand cmdAL = new SqlCommand(strSQLAccountInfo1,
cnSQLAccountInfo1);
cnSQLAccountInfo1 = new
SqlConnection(ConnectionstringAccountInfo1);
cnSQLAccountInfo1.Open();
SqlCommand cmSQLAccountInfo1;
cmSQLAccountInfo1 = new SqlCommand(strSQLAccountInfo1,
cnSQLAccountInfo1);
drSQLAccountInfo1 = cmSQLAccountInfo1.ExecuteReader();
int numbOfRowsAffected = 0;
numbOfRowsAffected =
drSQLAccountInfo1.RecordsAffected;
if (drSQLAccountInfo1.Read())
{
Class1.UseridCompany =
drSQLAccountInfo1["order_date"].ToString();
}
Any help is appreciated.
Thanks,
Trint
Nicholas Paldino [.NET/C# MVP] - 18 May 2007 15:45 GMT
When you execute a reader, you will not be able to get the number of
rows until you have finished cycling through the rows. In order to know the
number of rows beforehand, you will have to execute a call to the count
function in SQL server in order to get a record count.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
>I have a select statement that all I want to do is get the number of
> rows returned in my query:
[quoted text clipped - 29 lines]
> Thanks,
> Trint
trint - 18 May 2007 15:49 GMT
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
> When you execute a reader, you will not be able to get the number of
> rows until you have finished cycling through the rows. In order to know the
[quoted text clipped - 40 lines]
>
> - Show quoted text -
So, in this case, for me to do an increment (i++) while reading is the
best way?
Thanks,
Trint
Nicholas Paldino [.NET/C# MVP] - 18 May 2007 15:59 GMT
Yes, I would say so. If you need to know the value before you iterate
through the results, then you will need to issue the query twice, once to
get the count, once to get the actual results.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
> <m...@spam.guard.caspershouse.com> wrote:
[quoted text clipped - 48 lines]
> Thanks,
> Trint
Moty Michaely - 18 May 2007 16:06 GMT
On May 18, 5:59 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
> Yes, I would say so. If you need to know the value before you iterate
> through the results, then you will need to issue the query twice, once to
[quoted text clipped - 60 lines]
> > Thanks,
> > Trint
Hi,
SQL Server 2005 has functionalities for a RowID column and stuff like
you look for, so that might be helpful :)
Anyhow, aggregate functions is the only way I know.
About RecordsAffected: since the command executes a reader, the reader
is like a (forward only) cursor. So there is no way knowing the number
of rows prior to iterating through all the rows.
Cheers,
Moty
trint - 18 May 2007 16:52 GMT
> On May 18, 5:59 pm, "Nicholas Paldino [.NET/C# MVP]"
>
[quoted text clipped - 83 lines]
>
> - Show quoted text -
The increment worked! Thanks everyone.
Trint
Alberto Poblacion - 18 May 2007 15:47 GMT
>I have a select statement that all I want to do is get the number of
> rows returned in my query:
The easiest way would be to execute a "Select COUNT(*) from...". It's
best to use ExecuteScalar() rather than ExecuteNonQuery for this purpose.
The drSQLAccountInfo1.RecordsAffected that you are trying to use won't
work. It only counts the reccords for Insert, Update or Delete (not for
Select), and it only gives the result AFTER you have closed the datareader.
Dom - 18 May 2007 15:53 GMT
Forget CSharp and ADO for the moment. If you were doing this at Query
Analyzer, how would you get the number of rows? You need to use the
aggregate function, Count. The SQL statement is:
Select count (*)
from orders
where user_id = <???>
My experience has been that "RecordsAffected" is not very reliable
anyway. Don't know why.
Dom
> I have a select statement that all I want to do is get the number of
> rows returned in my query:
[quoted text clipped - 29 lines]
> Thanks,
> Trint
iLoreto - 18 May 2007 18:01 GMT
Trint:
I agree with Albert P. I hope this linik helps you,
See the remarks section.
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.execut
escalar.aspx

Signature
Ivan A Loreto
Application Analyst II
Loma Linda University Medical Center
Transplantation Institute
> I have a select statement that all I want to do is get the number of
> rows returned in my query:
[quoted text clipped - 29 lines]
> Thanks,
> Trint