SQL Server/C# Number of Rows...

T

trint

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
A

Alberto Poblacion

trint said:
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.
 
T

trint

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]
- (e-mail address removed)




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
 
D

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

trint said:
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]
- (e-mail address removed)




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
 
M

Moty Michaely

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]
- (e-mail address removed)


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]
- (e-mail address removed)

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
 
T

trint

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.
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
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]
- (e-mail address removed)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top