473,386 Members | 1,798 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,386 software developers and data experts.

number of row returned

Hello,
What is the easiest way to run a select SQL string and find out the number of row returned. If you give a code example it would be helpful?
Jim.

Nov 18 '05 #1
5 1569
This will return just the number of rows, no actual data.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted
Connection=True;")
conn.Open()
Dim cmd As New SqlCommand("Select Count(MyField) FROM MyTable", conn)
Dim result As String = cmd.ExecuteScalar().ToString
conn.Close()

If you use a dataadapter to fill a dataset you can access the datasets'
count property to get the number of records returned. This will return the
data and give you a row count.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted
Connection=True;")
conn.Open()
Dim da As New SqlDataAdapter("Select * from MyTable", conn)
Dim ds As New DataSet
da.Fill(ds)
Dim result As String = ds.Tables(0).Rows.Count.ToString
conn.Close

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hello,
What is the easiest way to run a select SQL string and find out the number
of row returned. If you give a code example it would be helpful?
Jim.

Nov 18 '05 #2
Or, better yet, run a stored procedure and in your stored procedure, declare
a couple integer variables, something like:

DECLARE @RC INT,
@ ERR INT

Then, in the next line after your select statement, use:

SELECT @RC = @@ROWCOUNT, @ERR = @@ERROR

Then, end your stored procedure with:

RETURN @RC

or, even better, use an output parameter to return the rowcount.

Note: I included @@ERROR along with @@ROWCOUNT above for a reason. You
should be checking for errors with every action in your stored proc, and
your stored procedure code should be heavily loaded with that statement
anyway.

Hope that helps,

Dale Preston
MCAD, MCSE, MCDBA

"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
This will return just the number of rows, no actual data.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted
Connection=True;")
conn.Open()
Dim cmd As New SqlCommand("Select Count(MyField) FROM MyTable", conn)
Dim result As String = cmd.ExecuteScalar().ToString
conn.Close()

If you use a dataadapter to fill a dataset you can access the datasets'
count property to get the number of records returned. This will return the
data and give you a row count.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted
Connection=True;")
conn.Open()
Dim da As New SqlDataAdapter("Select * from MyTable", conn)
Dim ds As New DataSet
da.Fill(ds)
Dim result As String = ds.Tables(0).Rows.Count.ToString
conn.Close

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hello,
What is the easiest way to run a select SQL string and find out the number of row returned. If you give a code example it would be helpful?
Jim.


Nov 18 '05 #3
Thanks for the reply, it is really helpful. how should I call this store procedure and get row count in my visual basic code. So, store procedure should accept one input parameter and return row count and I need to use this row count in my code.

"Dale" wrote:
Or, better yet, run a stored procedure and in your stored procedure, declare
a couple integer variables, something like:

DECLARE @RC INT,
@ ERR INT

Then, in the next line after your select statement, use:

SELECT @RC = @@ROWCOUNT, @ERR = @@ERROR

Then, end your stored procedure with:

RETURN @RC

or, even better, use an output parameter to return the rowcount.

Note: I included @@ERROR along with @@ROWCOUNT above for a reason. You
should be checking for errors with every action in your stored proc, and
your stored procedure code should be heavily loaded with that statement
anyway.

Hope that helps,

Dale Preston
MCAD, MCSE, MCDBA

"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
This will return just the number of rows, no actual data.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted
Connection=True;")
conn.Open()
Dim cmd As New SqlCommand("Select Count(MyField) FROM MyTable", conn)
Dim result As String = cmd.ExecuteScalar().ToString
conn.Close()

If you use a dataadapter to fill a dataset you can access the datasets'
count property to get the number of records returned. This will return the
data and give you a row count.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted
Connection=True;")
conn.Open()
Dim da As New SqlDataAdapter("Select * from MyTable", conn)
Dim ds As New DataSet
da.Fill(ds)
Dim result As String = ds.Tables(0).Rows.Count.ToString
conn.Close

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
Hello,
What is the easiest way to run a select SQL string and find out the number of row returned. If you give a code example it would be helpful?
Jim.



Nov 18 '05 #4
Here's a couple links to get you started:

http://msdn.microsoft.com/sql/sqlrel...onetprimer.asp

http://msdn.microsoft.com/library/de...et09102002.asp

Dale Preston
MCAD, MCSE, MCDBA
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
Thanks for the reply, it is really helpful. how should I call this store procedure and get row count in my visual basic code. So, store procedure
should accept one input parameter and return row count and I need to use
this row count in my code.
"Dale" wrote:
Or, better yet, run a stored procedure and in your stored procedure, declare a couple integer variables, something like:

DECLARE @RC INT,
@ ERR INT

Then, in the next line after your select statement, use:

SELECT @RC = @@ROWCOUNT, @ERR = @@ERROR

Then, end your stored procedure with:

RETURN @RC

or, even better, use an output parameter to return the rowcount.

Note: I included @@ERROR along with @@ROWCOUNT above for a reason. You
should be checking for errors with every action in your stored proc, and
your stored procedure code should be heavily loaded with that statement
anyway.

Hope that helps,

Dale Preston
MCAD, MCSE, MCDBA

"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
This will return just the number of rows, no actual data.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted Connection=True;")
conn.Open()
Dim cmd As New SqlCommand("Select Count(MyField) FROM MyTable", conn)
Dim result As String = cmd.ExecuteScalar().ToString
conn.Close()

If you use a dataadapter to fill a dataset you can access the datasets' count property to get the number of records returned. This will return the data and give you a row count.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted Connection=True;")
conn.Open()
Dim da As New SqlDataAdapter("Select * from MyTable", conn)
Dim ds As New DataSet
da.Fill(ds)
Dim result As String = ds.Tables(0).Rows.Count.ToString
conn.Close

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
> Hello,
> What is the easiest way to run a select SQL string and find out the

number
> of row returned. If you give a code example it would be helpful?
> Jim.
>


Nov 18 '05 #5
A couple more links.

http://msdn.microsoft.com/sql/sqlrel...l/gazoutas.asp

And the mother of all ADO.NET links:

http://msdn.microsoft.com/sql/sqlrel...t/default.aspx

Dale Preston
MCAD, MCSE, MCDBA

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
Thanks for the reply, it is really helpful. how should I call this store procedure and get row count in my visual basic code. So, store procedure
should accept one input parameter and return row count and I need to use
this row count in my code.
"Dale" wrote:
Or, better yet, run a stored procedure and in your stored procedure, declare a couple integer variables, something like:

DECLARE @RC INT,
@ ERR INT

Then, in the next line after your select statement, use:

SELECT @RC = @@ROWCOUNT, @ERR = @@ERROR

Then, end your stored procedure with:

RETURN @RC

or, even better, use an output parameter to return the rowcount.

Note: I included @@ERROR along with @@ROWCOUNT above for a reason. You
should be checking for errors with every action in your stored proc, and
your stored procedure code should be heavily loaded with that statement
anyway.

Hope that helps,

Dale Preston
MCAD, MCSE, MCDBA

"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
This will return just the number of rows, no actual data.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted Connection=True;")
conn.Open()
Dim cmd As New SqlCommand("Select Count(MyField) FROM MyTable", conn)
Dim result As String = cmd.ExecuteScalar().ToString
conn.Close()

If you use a dataadapter to fill a dataset you can access the datasets' count property to get the number of records returned. This will return the data and give you a row count.

Dim conn As New SqlConnection("Sever=MyServer;DataBase=MyDatabase; Trusted Connection=True;")
conn.Open()
Dim da As New SqlDataAdapter("Select * from MyTable", conn)
Dim ds As New DataSet
da.Fill(ds)
Dim result As String = ds.Tables(0).Rows.Count.ToString
conn.Close

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
> Hello,
> What is the easiest way to run a select SQL string and find out the

number
> of row returned. If you give a code example it would be helpful?
> Jim.
>


Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: sphincter66 | last post by:
Hi I'm trying to write a function that will accept an ID number and depending on certain criteria (that's what I need help with) it will return a number between a specified range (lets say...
21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
9
by: Adam Knight | last post by:
I am dividing a value by two..what i need is to round the number up if it is not a whole number. Any suggestions on how to do this ?? Help appreciated!! AK
3
by: CSDunn | last post by:
Hello, I have a situation with MS Access 2000 in which I need to display report data in spreadsheet orientation (much like a datasheet view for a form). If you think of the report in terms of what...
4
by: Ed | last post by:
I have a new requisite from a client that wants to give the user the option to about a databind based on number of returned rows. if ds.rows.count < 1000 then dg.databind() etc. They want...
4
by: Chris Tremblay | last post by:
I am trying to figure out how to go about retrieving the number of results returned from my queries in SQL server from VB.NET without using a the Select Count(*) query. The method that I was using...
5
by: dananrg | last post by:
I was messing around with the native ODBC module (I am using Python in a Win32 environment), e.g: import dbi, odbc ....and it seems to meet my needs. I'd rather use a module that comes...
0
by: Trak | last post by:
I had written coding to fetch line number of an exception. The line number is returned as 0 when the line number cannot be determined. This works fine with normal forms, where the correct line number...
2
by: yuva17raj | last post by:
hi i am using the select query with group by, so that i am getting records with more than one column so that i need to calculate the number of rows returned by the query for example with my...
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: 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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.