473,378 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

SQL Server/C# Number of Rows...

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
8 18535
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
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Is it possible to auto-number rows of a table, similar to how <LI> is used to number a series of lines?
19
by: xixi | last post by:
i want to use sql query to open a resultset, say i want the cursor point to whatever position i start with by giving a row number , so is there anyway i can use the sql function to do that, so...
0
by: John A Grandy | last post by:
In ASP.NET 2.0 , does the ObjectDataSource provide a method or property to return the number of rows returned ?
4
by: esmith2112 | last post by:
I have a situation that I can't explain. Boiled down to its essence, I have a query of the form SELECT A.COL1, A.COL2, B.COL1 FROM A LEFT JOIN B ON A.KEY = B.KEY This query produces 5383...
9
by: dotnetguru | last post by:
Hi SMART GUYS, Please help me write a query. Actually I want to convert my rows into columns. Can anyone kindly give me the query to do it? My rows are about employees. There can be any number of...
1
by: sharadadutt1981 | last post by:
hi all, I was trying to fetch even or odd number rows from database. currently i am usying DB2 ver 8.0. any one can help me out.
1
by: clickingwires | last post by:
How do you consecutively number rows in an aggregate query?
4
by: JovieUrbano | last post by:
I have a question. I am working on something based on the information I got from this forum. I am using the sample I found and modifying it a little to get the results I want.. --TABLE--...
4
by: yuvaly | last post by:
Hi I want to Query a database using AJAx and display the results in the middle rows of an existing table. right now the server builds rows and return them to the main page, but I don't know how to...
1
by: jeddiki | last post by:
Hi, I want to get a subset from my table that includes rows that have an item (cb_id) with a unique ip address ( ip_adr). To be in the subset there should be at least two rows and if there are more...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.