Solved

ExecuteNonQuery doesn't return Count, what else to use

Posted on 2007-03-26
5
863 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
ID: 18797805
it Executes a Transact-SQL statement against the connection and returns the "number of rows affected"
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18797807
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18797824
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
ID: 18799491
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
ID: 18801647
Thanks for inputs.  The "problem" is my acute newbie-itis in this area... :-)
'Preciate the pointers!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

680 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