Solved

ExecuteNonQuery doesn't return Count, what else to use

Posted on 2007-03-26
5
856 Views
Last Modified: 2012-06-22
I thought I'd use this construct

Using wrkConnection As New SqlConnection(ConfigurationManager.ConnectionStrings(1).ToString)
Dim wrkSQLCmd As New SqlCommand(varSQLString, wrkConnection)
wrkSQLCmd.Connection.Open()
varRowCnt = wrkSQLCmd.ExecuteNonQuery()
End Using

to execute a Select Count or (simple Select query)...but I'm confused because MSDN says .ExecuteNonQuery() returns -1 except for Delete, update, insert.

What should I be using instead?  I'd like to be able to pass the query string to this construct in a subroutine and have it return the count in a Byref...but mostly I've got to get something that will execute the string and return the count.

Any help with this would be appreciated.

Thanks!
0
Comment
Question by:codequest
  • 4
5 Comments
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
it Executes a Transact-SQL statement against the connection and returns the "number of rows affected"
0
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
0
 
LVL 21

Expert Comment

by:theGhost_k8
Comment Utility
i don't understand what is the prob actually.
if you want the row count then you can do executescaler that will return the first value..
eg.
your query:- select  count(*) from emp
so 1st value:- row-count
ExecuteScalar will return only on value that is first column value of the first row in the executed query.
0
 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 500 total points
Comment Utility
havent checked programtically, but will work....

Pass query: select  count(*) from Table_Name;
YOUR SUB_ROUTINE
Using wrkConnection As New SqlConnection(ConfigurationManager.ConnectionStrings(1).ToString)
Dim wrkSQLCmd As New SqlCommand(varSQLString, wrkConnection)
wrkSQLCmd.Connection.Open()
varRowCnt = wrkSQLCmd.executescaler()
End Using
RETURN varRowCnt
0
 
LVL 2

Author Comment

by:codequest
Comment Utility
Thanks for inputs.  The "problem" is my acute newbie-itis in this area... :-)
'Preciate the pointers!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now