473,618 Members | 3,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 strSQLAccountIn fo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUse rid.Trim() +
"' ";

SqlCommand cmdAL = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
cnSQLAccountInf o1 = new
SqlConnection(C onnectionstring AccountInfo1);
cnSQLAccountInf o1.Open();

SqlCommand cmSQLAccountInf o1;
cmSQLAccountInf o1 = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
drSQLAccountInf o1 = cmSQLAccountInf o1.ExecuteReade r();

int numbOfRowsAffec ted = 0;
numbOfRowsAffec ted =
drSQLAccountInf o1.RecordsAffec ted;

if (drSQLAccountIn fo1.Read())
{

Class1.UseridCo mpany =
drSQLAccountInf o1["order_date "].ToString();
}

Any help is appreciated.
Thanks,
Trint

May 18 '07 #1
8 18545
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.co m

"trint" <tr***********@ gmail.comwrote in message
news:11******** **************@ u30g2000hsc.goo glegroups.com.. .
>I have a select statement that all I want to do is get the number of
rows returned in my query:

string strSQLAccountIn fo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUse rid.Trim() +
"' ";

SqlCommand cmdAL = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
cnSQLAccountInf o1 = new
SqlConnection(C onnectionstring AccountInfo1);
cnSQLAccountInf o1.Open();

SqlCommand cmSQLAccountInf o1;
cmSQLAccountInf o1 = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
drSQLAccountInf o1 = cmSQLAccountInf o1.ExecuteReade r();

int numbOfRowsAffec ted = 0;
numbOfRowsAffec ted =
drSQLAccountInf o1.RecordsAffec ted;

if (drSQLAccountIn fo1.Read())
{

Class1.UseridCo mpany =
drSQLAccountInf o1["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.guar d.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.c om

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

news:11******** **************@ u30g2000hsc.goo glegroups.com.. .
I have a select statement that all I want to do is get the number of
rows returned in my query:
string strSQLAccountIn fo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUse rid.Trim() +
"' ";
SqlCommand cmdAL = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
cnSQLAccountInf o1 = new
SqlConnection(C onnectionstring AccountInfo1);
cnSQLAccountInf o1.Open();
SqlCommand cmSQLAccountInf o1;
cmSQLAccountInf o1 = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
drSQLAccountInf o1 = cmSQLAccountInf o1.ExecuteReade r();
int numbOfRowsAffec ted = 0;
numbOfRowsAffec ted =
drSQLAccountInf o1.RecordsAffec ted;
if (drSQLAccountIn fo1.Read())
{
Class1.UseridCo mpany =
drSQLAccountInf o1["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.goo glegroups.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 drSQLAccountInf o1.RecordsAffec ted 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 "RecordsAffecte d" 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 strSQLAccountIn fo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUse rid.Trim() +
"' ";

SqlCommand cmdAL = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
cnSQLAccountInf o1 = new
SqlConnection(C onnectionstring AccountInfo1);
cnSQLAccountInf o1.Open();

SqlCommand cmSQLAccountInf o1;
cmSQLAccountInf o1 = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
drSQLAccountInf o1 = cmSQLAccountInf o1.ExecuteReade r();

int numbOfRowsAffec ted = 0;
numbOfRowsAffec ted =
drSQLAccountInf o1.RecordsAffec ted;

if (drSQLAccountIn fo1.Read())
{

Class1.UseridCo mpany =
drSQLAccountInf o1["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.co m

"trint" <tr***********@ gmail.comwrote in message
news:11******** *************@y 80g2000hsf.goog legroups.com...
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om

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

news:11******* *************** @u30g2000hsc.go oglegroups.com. ..
>I have a select statement that all I want to do is get the number of
rows returned in my query:
string strSQLAccountIn fo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUse rid.Trim() +
"' ";
SqlCommand cmdAL = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
cnSQLAccountInf o1 = new
SqlConnection(C onnectionstring AccountInfo1);
cnSQLAccountInf o1.Open();
SqlCommand cmSQLAccountInf o1;
cmSQLAccountInf o1 = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
drSQLAccountInf o1 = cmSQLAccountInf o1.ExecuteReade r();
int numbOfRowsAffec ted = 0;
numbOfRowsAffec ted =
drSQLAccountInf o1.RecordsAffec ted;
if (drSQLAccountIn fo1.Read())
{
Class1.UseridCo mpany =
drSQLAccountInf o1["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.guar d.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.c om

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

news:11******** *************@y 80g2000hsf.goog legroups.com...
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om
"trint" <trinity.sm...@ gmail.comwrote in message
>news:11******* *************** @u30g2000hsc.go oglegroups.com. ..
I have a select statement that all I want to do is get the number of
rows returned in my query:
string strSQLAccountIn fo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUse rid.Trim() +
"' ";
SqlCommand cmdAL = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
cnSQLAccountInf o1 = new
SqlConnection(C onnectionstring AccountInfo1);
cnSQLAccountInf o1.Open();
SqlCommand cmSQLAccountInf o1;
cmSQLAccountInf o1 = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
drSQLAccountInf o1 = cmSQLAccountInf o1.ExecuteReade r();
int numbOfRowsAffec ted = 0;
numbOfRowsAffec ted =
drSQLAccountInf o1.RecordsAffec ted;
if (drSQLAccountIn fo1.Read())
{
Class1.UseridCo mpany =
drSQLAccountInf o1["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.guar d.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.c om
"trint" <trinity.sm...@ gmail.comwrote in message
news:11******** *************@y 80g2000hsf.goog legroups.com...
On May 18, 10:45 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om
>"trint" <trinity.sm...@ gmail.comwrote in message
>>news:11****** *************** *@u30g2000hsc.g ooglegroups.com ...
>I have a select statement that all I want to do is get the number of
rows returned in my query:
string strSQLAccountIn fo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUse rid.Trim() +
"' ";
SqlCommand cmdAL = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
cnSQLAccountInf o1 = new
SqlConnection(C onnectionstring AccountInfo1);
cnSQLAccountInf o1.Open();
SqlCommand cmSQLAccountInf o1;
cmSQLAccountInf o1 = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
drSQLAccountInf o1 = cmSQLAccountInf o1.ExecuteReade r();
int numbOfRowsAffec ted = 0;
numbOfRowsAffec ted =
drSQLAccountInf o1.RecordsAffec ted;
if (drSQLAccountIn fo1.Read())
{
Class1.UseridCo mpany =
drSQLAccountInf o1["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 strSQLAccountIn fo1 = "select * " +
"FROM orders " +
"where user_id = '" + Class1.loginUse rid.Trim() +
"' ";

SqlCommand cmdAL = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
cnSQLAccountInf o1 = new
SqlConnection(C onnectionstring AccountInfo1);
cnSQLAccountInf o1.Open();

SqlCommand cmSQLAccountInf o1;
cmSQLAccountInf o1 = new SqlCommand(strS QLAccountInfo1,
cnSQLAccountInf o1);
drSQLAccountInf o1 = cmSQLAccountInf o1.ExecuteReade r();

int numbOfRowsAffec ted = 0;
numbOfRowsAffec ted =
drSQLAccountInf o1.RecordsAffec ted;

if (drSQLAccountIn fo1.Read())
{

Class1.UseridCo mpany =
drSQLAccountInf o1["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
10944
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
81798
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 when the resultset return, the first row will be the absolute row dertermine by the row number
0
1071
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
1537
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 rows of output. Because B.COL1 was defined as VARCHAR(2000), it was making my ouput file too hard to browse so I eliminated it from the select clause.
9
101431
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 employees (in the following table I have 3 employees ABC, EFG, WRI) with any number of records for each employee (there can be 20, 30 records each), but all employees have the same number of records (in the following table each employee has 3...
1
9405
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
1833
by: clickingwires | last post by:
How do you consecutively number rows in an aggregate query?
4
3787
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-- Create table test (PersonID varchar(2), Degree varchar(10)) insert into test values ('55', 'MD')
4
1619
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 insert them to the right spot. basically, I look for a function he inserts a string before /after some row in the table . in the pad I have: <table> <tr><td>Header</td></tr> <tr><td>footer</td></tr></table
1
2088
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 than four rows then the rows above the forth should be filtered out. eg. table contains: EBFORTUNE, IP add: 55.167.889.182 EBFORTUNE, 78.167.109.182 FEDYDE, : 85.106.68.197 FEDYDE, 85.176.68.227 FEDYDE, 89.106.68.127
0
8207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8150
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8650
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8593
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7124
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5552
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4064
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.