By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

SQL Server/C# Number of Rows...

P: n/a
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

May 18 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"trint" <tr***********@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
>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

May 18 '07 #2

P: n/a
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"trint" <trinity.sm...@gmail.comwrote in message

news:11**********************@u30g2000hsc.googlegr oups.com...
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- Hide quoted text -

- Show quoted text -
So, in this case, for me to do an increment (i++) while reading is the
best way?
Thanks,
Trint

May 18 '07 #3

P: n/a
"trint" <tr***********@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
>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.
May 18 '07 #4

P: n/a
Dom
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

On May 18, 10:25 am, trint <trinity.sm...@gmail.comwrote:
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

May 18 '07 #5

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"trint" <tr***********@gmail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> 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.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"trint" <trinity.sm...@gmail.comwrote in message

news:11**********************@u30g2000hsc.googleg roups.com...
>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- Hide quoted text -

- Show quoted text -

So, in this case, for me to do an increment (i++) while reading is the
best way?
Thanks,
Trint

May 18 '07 #6

P: n/a
On May 18, 5:59 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"trint" <trinity.sm...@gmail.comwrote in message

news:11*********************@y80g2000hsf.googlegro ups.com...
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"trint" <trinity.sm...@gmail.comwrote in message
>news:11**********************@u30g2000hsc.googleg roups.com...
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- Hide quoted text -
- Show quoted text -
So, in this case, for me to do an increment (i++) while reading is the
best way?
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

May 18 '07 #7

P: n/a
On May 18, 11:06 am, Moty Michaely <Moty...@gmail.comwrote:
On May 18, 5:59 pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
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.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"trint" <trinity.sm...@gmail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> 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.
>--
> - Nicholas Paldino [.NET/C# MVP]
> - m...@spam.guard.caspershouse.com
>"trint" <trinity.sm...@gmail.comwrote in message
>>news:11**********************@u30g2000hsc.google groups.com...
>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- Hide quoted text -
>- Show quoted text -
So, in this case, for me to do an increment (i++) while reading is the
best way?
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- Hide quoted text -

- Show quoted text -
The increment worked! Thanks everyone.
Trint

May 18 '07 #8

P: n/a
Trint:

I agree with Albert P. I hope this linik helps you,
See the remarks section.

http://msdn2.microsoft.com/en-us/lib...utescalar.aspx

--
Ivan A Loreto
Application Analyst II
Loma Linda University Medical Center
Transplantation Institute
"trint" wrote:
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

May 18 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.